Visit Our UNIX and Linux User Community


Insert text at line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text at line number
# 1  
Old 07-24-2009
Insert text at line number

I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance. I have this

Code:
INSERT_NODE='<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" />'
GREP_FOR='</JOB>'
TMP_FILE=/tmp/lineArray.$$
if [[ -e ${DRF_PATH} ]]; then
 continue
else
 echo "File ${DRF_PATH} does not exist."
 exit 1
fi
grep -n "${GREP_FOR}" ${DRF_PATH} | cut -d: -f1 > ${TMP_FILE}
cat ${TMP_FILE} | while read LINE
do
 set -A LINE_ARRAY ${LINE_ARRAY[*]} $LINE
done

((COUNT=0))
while (( ${COUNT} < ${#LINE_ARRAY[*]} ))
do
 INSERT_LINE=${LINE_ARRAY[${COUNT}]}
 ((INSERT_LINE=${INSERT_LINE}-1))
 echo "[Insert]  ${INSERT_NODE} at line ${INSERT_LINE}"
 ((COUNT=${COUNT}+1))
done
exit 0

And I have the info I need now, except the actual insert part
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 75
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 149
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 228
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 300
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 364
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 428
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 518
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 569
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 644
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 709
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 782
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 861
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 912
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 985
[Insert] <QUANTITATIVE NAME="DB-BLODS" QUANT="1" /> at line 1058
...

---------- Post updated at 07:00 PM ---------- Previous update was at 06:55 PM ----------

I was reading up on awk, but now it looks like maybe I should use sed. Still reading the man file though.

---------- Post updated at 07:20 PM ---------- Previous update was at 07:00 PM ----------

Those didnt help, looking at ed now.
# 2  
Old 07-24-2009
Yes, with "sed" its very easy.
You have given the output you have now.
Give the output that you want it to be.
# 3  
Old 07-24-2009
sed wont work its not a replace

The text example would look like

Code:
75:    </ON>
76:   </JOB>
77:   <JOB
78:    APR="1"

So I have my new insert string
<QUANTITATIVE NAME="DB-BLODS" QUANT="1" />

And I need to stick that in after line 75
# 4  
Old 07-24-2009
Wow.... you learnt all about sed in 10 minutes.
That was pretty fast.

Again, if you want a solution, do this.
post what you have and what you need.
Example:
I get:
Code:
 
aaa
bbb
ccc
ddd

I want it as follows:
Code:
 
aaa
bbb
11111111
ccc
ddd

11111111 must come after bbb
Or may be
11111111 must come before ccc
# 5  
Old 07-25-2009
My problem with sed, and its my inexperience with the command is sed syntax

sed '/abc/123/' <file name>

All the xml tags and inserts have their own / part of the xml syntax.

So I'm thinking something like

Code:
 ed - ${DRF_PATH} << EOF
 ${INSERT_LINE}
 ${INSERT_NODE}
 w
 q
 EOF

But I havent tested it.

---------- Post updated at 09:11 PM ---------- Previous update was at 08:23 PM ----------

Code:
((COUNT=0))
while (( ${COUNT} < ${#LINE_ARRAY[*]} ))
do
 INSERT_LINE=${LINE_ARRAY[${COUNT}]}
 ((INSERT_LINE=${INSERT_LINE}-1))
 echo "[Insert]  ${INSERT_NODE} at line ${INSERT_LINE}"
 ed - ${DRF_PATH} << EOF
 ${INSERT_LINE}a
 ${INSERT_NODE}
 .
 w
 q
 EOF
 ((COUNT=${COUNT}+1))
done
exit 0

Ok that does what I want, but works manually but not in the script.

$ ed - ../BIL-EXF.DRF.xml << EOF
> 75a
> <QUANTITATIVE NAME="DB-ODSAL" QUANT="1" />
> .
> w
> q
> EOF

Makes the insert I need at the right spot, but when run in the script it failes.

Quote:
$ ./InsertQRinDRF.sh -i ../BIL-EXF.DRF.xml -q DB-ODSAL
./InsertQRinDRF.sh[54]: syntax error at line 59 : `<' unmatched
59: ed - ${DRF_PATH} << EOF
# 6  
Old 07-25-2009
Quote:
Originally Posted by J-Man
I wrote a script to grep for a closing XML node. Then I need it to navigate up a line and insert some XML. Then go to the next occurrance.

Code:
insert="<xxx>XML to insert</xxx>"
awk -v insert="$insert" '
  /<\/whatever>/ { print insert }
                { print }
   ' FILENAME

# 7  
Old 07-25-2009
Thanks cfajohnson and edidataguy.

I did finally get it to work with sed, thanks.

Code:
GREP_FOR='<\/JOB>'

sed '
/'${GREP_FOR}'/ i\
<QUANTITATIVE NAME="'${QR_NAME}'" QUANT="1" \/>
' ${DRF_PATH} > ${DRF_PATH}.tmp
mv ${DRF_PATH}.tmp ${DRF_PATH}


Previous Thread | Next Thread
Test Your Knowledge in Computers #342
Difficulty: Easy
Sun's first Unix workstation was called SunOS-1.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

3. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

4. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

5. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

6. Shell Programming and Scripting

Insert new line based on numerical number of column

My input file: Class Number Position Range 1 Initial 50 1 Initial 50 2 Terminal 150 2 Terminal 20 2 Single 10 3 Single 20 4 Double 50 5 Initial 50 5 Initial 60 Class Number... (11 Replies)
Discussion started by: patrick87
11 Replies

7. Shell Programming and Scripting

Insert output into file at line number

I need to insert the output of a script into a specific line number of a txt file. I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this. (4 Replies)
Discussion started by: pluto7777
4 Replies

8. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

9. Shell Programming and Scripting

sed/awk to insert comment at defined line number

Hi there, may someone easily help me on this : I want to insert a text in a specific line number like : linenumb2start=`cat memory_map.dld | nl -ba | egrep -i "label" | cut -f1` line2insert=`expr $linenumb2start + 2` and now I need to replace something like {} with {comment} at... (8 Replies)
Discussion started by: homefp
8 Replies

10. UNIX for Dummies Questions & Answers

how to insert a line number on every line

hi... i have a file with data and would like to insert a number and bracket 1) ...2) at the beginning of every successive line; to add some formatting to the text (3 Replies)
Discussion started by: mopimp
3 Replies

Featured Tech Videos