开发者

Append to end of line to specific lines

开发者 https://www.devze.com 2023-04-09 02:54 出处:网络
I am writing a shell script and need some help. This script will take a开发者_开发知识库 file name as input and will modify that input file according to some logic.

I am writing a shell script and need some help.

This script will take a开发者_开发知识库 file name as input and will modify that input file according to some logic.

Now, this file will be modified only specific lines, I will get to know the line number though.

So after getting to know the line number, how do I append some text to end of that line?

The text to be appended will keep changing for each line.

Example : The input files contain a list of file names.

Input file is abc.txt, with content:

a.c;
b.c;
c.c;
d.c;

This abc.txt is input to the script, the script should pick up the file names from abc.txt and check where the files are present in a specified directory.

I have another script which gives the name of the directory if the file is there in that location.

If the output of other script is some folder name then it should modify the input file.

For example, if a.c and c.c files are present in dirctory 'somefolder1' and 'somefolder2' (respectively), then expected output is:

a.c;somefolder1;
b.c;
c.c;somefolder2;
d.c;

This is the logic I am using:

MAX_LINES=`wc -l ${CHECKOUT_FILE}.tmp |awk '{print $1}'`
COUNTER=0

#Logic
while [ $COUNTER -ne $MAX_LINES ]
do
    COUNTER=`expr $COUNTER + 1`
    LINE_READ=`sed -n ${COUNTER}p ${CHECKOUT_FILE}.tmp`
    PROGRAM_NAME=`echo $LINE_READ | cut -f 1 -d ";" `
    FOLDER_NAME=`${CLEARCASE}/somescript.sh ${PROGRAM_NAME} ${CHECKOUT_COUNTRY}`

    if [ $FOLDER_NAME = 'NOTFOUND' ] ; then
        echo $FOLDER_NAME
        continue
    else
        sed -e '${COUNTER}s/$/${FOLDER_NAME};/' < ${CHECKOUT_FILE}.tmp > temp.txt
    fi
done


I don't know how to do it in sh, but I've done a little Python script:

#!/usr/bin/env python
import sys


mapping = {
    2: 'second',
    4: 'fourth',
}


def main(src, dst):
    with open(src, 'r') as f:
        lines = f.readlines()
    new_lines = process(lines)
    with open(dst, 'w') as f:
        f.write(''.join(new_lines))


def process(lines):
    for l, s in mapping.items():
        lines[l+1] = '%s %s\n' % (lines[l+1].rstrip('\n'), s)
    return lines


if __name__ == '__main__':
    main(sys.argv[1], sys.argv[2])

It's easy to use: first you define the mapping on top of the script, where you have a

line_number: 'text to append'

pairs. Then save the script as append.py and run it as

python append.py src dst

where src is the filename you want to process, dst is the filename to save.


Try this:

$ linenum=4
$ sed -e "${linenum}s/\$/text to add/" < input.txt > output.txt


you can include this inside a loop like

awk '{if(NR==<your line number>)print $0,<your additional text>;else print}' your_file >>temp_file
rm your_file
mv temp_file your_file

NR stands for line number in awk.so we are checking for NR == lets say line 3. if NR is 3 the
print $0,"your text" $0 is complete line which will be appended by "your text" if not line 3 the else{print}-this will print the complete line.

Or a more easy form in sed :

pearl[ncm_o11.2_int.@].525> cat file1
a
b
c
d
e
f
pearl[ncm_o11.2_int.@].526> sed '1s/$/vijay/g' file1
avijay
b
c
d
e
f

This sed command will append to the end of the line the desired text.

'<line_number>s/search/replace'
'     1        s/  $   / vijay  '  

here is the awk solution:

pearl[ncm_o11.2_int.@].534> awk '{if(NR==1)print $0,"vijay";else print}' file1
a vijay
b
c
d
e
f
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号