Use shell script to insert a record every 100 rows and record the first column contains row number other columns unchanged _linux shell

Source: Internet
Author: User
Tags first row

1. Example: Insert a record every 100 rows, and the first column of this record contains the line number, and the other columns are the same as the first row

#!/bin/sh
var= "00001" # variable
cat A.txt|awk ' nr==1 ' |while read Line # Get the first row of the file do
    #echo "$line"
    result= ' Echo ' $line ' |cut-f2-' # Defaults to tab split to get the second column to the last column of
    cat A.txt|awk ' nr%100==0{printf ("%04d\n", NR)} ' |while Read line # Get the line number every 100 lines, the line number is 4 bits, the insufficient digits complement 0 do
        data= ' echo-e ' 9$line$var\t$result ' "
        echo" $line "
        #echo" $result "
        #echo "$data"
        #sed-E "' $line ' a\ ' $data '" a.txt
        sed-i ' $line ' a ' "$data" ' A.txt # Insert
done to file Done

2, example: Every 100 lines, replace the 100th row of data

#!/bin/sh
var= "abc"
Cat A.txt|awk ' nr%100==0 ' |awk ' {split ($0,b, "T");p rint b[2]} ' |while read line # reads a file, Get the second column of the current row
    #echo "$line"
    sed-i "s/$line/$var/g" A.txt # every 100 rows replaces the obtained column with known variables done

The above is a small set for you to introduce the use of shell script to achieve every 100 rows inserted a record and record the first column contains row number other columns unchanged, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.