[string processing]Adding new line in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [string processing]Adding new line in file
# 1  
Old 09-16-2011
[string processing]Adding new line in file

I have a report file which somewhere has a lines starting with word ".sub"

I have a new variable named $new.
What i am trying to do is insert the content of $new just before the occurrence of ".sub" in the report file.

How can I do that?
# 2  
Old 09-16-2011
Code:
sed "s/^.sub/$new.sub/" file > newfile

# 3  
Old 09-16-2011
Hey, Franklin thanks..
actually I needed the $new so be printed a line before occurence of .subckt

earlier file
Code:
a
b
c
.sub efgh

new file
Code:
a
b
c
$new.sub
.sub efgh

# 4  
Old 09-16-2011
Hi,
please find the below code, hope this might work for your need.
but this will insert the content of $new just before the coourance of ".sub" in the report file for all the lines which are starting with ".sub" only.

Code:
 
 sed -i "s/^.sub/$new.sub/g" report_file

if you want to insert everywhere in file where ever the string occurs,
you can use the below code:
Code:
 
sed -i "s/.sub/$new.sub/g" report_file

Hope you got these....
# 5  
Old 09-16-2011
Quote:
Originally Posted by animesharma
Hey, Franklin thanks..
actually I needed the $new so be printed a line before occurence of .subckt

earlier file
Code:
a
b
c
.sub efgh

new file
Code:
a
b
c
$new.sub
.sub efgh

Code:
awk -v var=$new '/^.sub/{print var ".sub"}1' file > newfile

# 6  
Old 09-16-2011
hey thanks..
it works Smilie
# 7  
Old 09-19-2011
Hey Guys, I am trying to add a new requirement to the query above..
I will explain it with an example:

Original String
Code:
a
b
c
.sub efgh
d
e
.sub fgh

Edited String : The New line is added only before the line with first .sub statement. Hence even if .sub statement occurs for 100 times, the new line "$new" will be added only before the first .sub statement.
Code:
a
b
c
$new.sub
.sub efgh
d
e
.sub fgh

Hence.. the requirement is: how can I make the awk statement (as given by Franklin) exit after the occurrence of first .sub statement.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding line in a file using info from previous line

I have a shell script that looks something like the following: mysql -uroot db1 < db1.sql mysql -uroot db2 < db2.sql mysql -uroot db3 < db3.sql mysql -uroot db4 < db4.sql .... different db names in more than 160 lines. I want to run this script with nohup and have a status later. So,... (6 Replies)
Discussion started by: MKH
6 Replies

2. UNIX for Advanced & Expert Users

Help in adding a string at the end of each line and append files vertically

hi, i need a help in the script , need to append a string at the end of each line of a files , and append the files into a single file vertically. eg file1 has the following columns abc,def,aaa aaa,aa,aaa files 2 has the following rows and columns abc,def,aaa aaa,aa,aaa i... (3 Replies)
Discussion started by: senkerth
3 Replies

3. Shell Programming and Scripting

Adding a String after a text in a Line (using nawk)

Hello I need to add a String after a text in a line. The Unix file is huge and I think nawk would be quick. Current: -name FILTER -node 60265 -cob 31/01/2013 -risktype + -change 1 -filter ALL_NODES -toponly -warnings OFF -delimiter "|" -noheader -select... (4 Replies)
Discussion started by: filter
4 Replies

4. Shell Programming and Scripting

[awk] line by line processing the same file

Hey, not too good at this, so I only managed a clumsy and SLOW solution to my problem that needs a drastic speed up. Any ideas how I write the following in awk only? Code is supposed to do... For every line read column values $6, $7, $8 and do a calculation with the same column values of every... (6 Replies)
Discussion started by: origamisven
6 Replies

5. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

6. Shell Programming and Scripting

Help with File processing - Adding predefined text to particular record based on condition

I am generating a output: Name Count_1 Count_2 abc 12 12 def 15 14 ghi 16 16 jkl 18 18 mno 7 5 I am sending the output in html email, I want to add the code: <font color="red"> NAME COLUMN record </font> for the Name... (8 Replies)
Discussion started by: karumudi7
8 Replies

7. UNIX for Dummies Questions & Answers

Adding one string at the beginning of each line in a file

Hi, I have file a.txt as below. I want to add one string root beginning of each line. Sample file a.txt aaa bbb ccc Sample output Root aaa Root bbb Root ccc Can any one help me on this? (6 Replies)
Discussion started by: siba.s.nayak
6 Replies

8. Shell Programming and Scripting

reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt. and i am stuck. list=`cat /u/Test/programs`; psg "ServTest" | awk -v listawk=$list '{ cmd_name=($5 ~ /^/)? $9:$8 for(pgmname in listawk) ... (6 Replies)
Discussion started by: Anteus
6 Replies

9. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies

10. Shell Programming and Scripting

processing line in file

Hi I amtrying to read the lines from a file, these lines are absolute paths in the system. I want to check if these paths exists, if they doesn't I want to create that path and put a file in that location/path. I had no trouble filtering these paths out using awk, grep, uniq etc but when it... (8 Replies)
Discussion started by: fablef00
8 Replies
Login or Register to Ask a Question