Computing.Net > Forums > Unix > insert lines from a file to diff fi

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

insert lines from a file to diff fi

Reply to Message Icon

Name: Pradeepa
Date: November 14, 2008 at 02:43:23 Pacific
OS: UNIX
CPU/Ram: 1GB
Comment:

Hi,

I have a file with a HDR, some lines (number of lines can vary) and a TRL. And this set is repeating. I have to insert 2 lines just after the TRL, but not after the last TRL. I was able to do it using SED, But as the business system does not support the SED, I have to rewrite the script. and the system supports awk. Please help.

The file is like,
HDR
---
---
---
TRL
HDR
---
---
TRL
and expected is

HDR
---
---
---
TRL
1st line
2nd line
HDR
----
----
TRL



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 14, 2008 at 08:55:59 Pacific
Reply:

This uses just the korn shell:


#!/bin/ksh


cnt=$(wc -l < datafile)
echo $cnt
ncnt=0
while read line
do
((ncnt+=1))
echo "$line"
if [[ "$line" = "TRL" && $ncnt -ne $cnt ]]
then
echo "line 1"
echo "line 2"
fi
done < datafile > newdatafile



0

Response Number 2
Name: Pradeepa
Date: November 18, 2008 at 00:57:48 Pacific
Reply:

Hi nails,
thanks a lot for the solution.
I have one more problem here which i did not mention. There will be more text after TRL, like "TRL 00 123 1235"
i have to check it the line starts with "TRL and then do the rest. I tried using ^TRL, /^TRL/, it did not work. Please help me.

Thanks a lot
Pradeepa


0

Response Number 3
Name: nails
Date: November 18, 2008 at 08:58:24 Pacific
Reply:

How about trying the case statement:


#!/bin/ksh

cnt=$(wc -l < data2)
echo $cnt
ncnt=0
while read line
do
((ncnt+=1))
echo "$line"
case "$line" in
TRL*)
if [[ $ncnt -ne $cnt ]]
then
echo "line 1"
echo "line 2"
fi
;;
esac
done < datafile



0

Response Number 4
Name: Pradeepa
Date: November 20, 2008 at 22:27:09 Pacific
Reply:

hey nails,

thanks a lot, It works.


0

Response Number 5
Name: Pradeepa
Date: December 4, 2008 at 02:15:34 Pacific
Reply:

Hey,

when I do an echo the trailing tab spaces are removed,
for example when i have the text
"This is Testing tab <tabspace> "
Could not press a tab....
When I do an echo of this, the tabspace is removed. Please help me as i want this tabspace after this.

Thank you


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: insert lines from a file to diff fi

Get the first line from a file www.computing.net/answers/unix/get-the-first-line-from-a-file/7981.html

Script to Remove lines from a file www.computing.net/answers/unix/script-to-remove-lines-from-a-file/5268.html

getting a specific line from a file www.computing.net/answers/unix/getting-a-specific-line-from-a-file/7708.html