[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
# 8  
Old 09-19-2011
Quote:
Originally Posted by animesharma
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.
Something like this?
Code:
awk -v var=$new '/^.sub/ && !c++{print var ".sub"}1' file > newfile

# 9  
Old 09-19-2011
Code:
awk -v var=$new '/^.sub/ && !c++{print var ".sub"}1' file > newfile

Hey Franklin, what does !c++ mean here?
The c++ keywords is giving too many non relevent search on google.

Thanks

---------- Post updated at 05:20 PM ---------- Previous update was at 05:17 PM ----------

While running your awk line, I am getting following error

Code:
c++: Event not found

Since I am not aware of what C++ means here, am not able to troubleshoot it Smilie

---------- Post updated at 05:33 PM ---------- Previous update was at 05:20 PM ----------

Hey done it another way
Code:
set cnt = 0
awk -v var="$new" '/^.sub/{if(cnt < 1) print var; cnt++ }1' file1 > file2

Thanks for your help. Smilie
# 10  
Old 09-19-2011
!c++ means if c==0 (the 1st match, before the increment of the variable c) so this may work on your system:
Code:
awk -v var="123" '/^.sub/ && c++==0{print var ".sub"}1' file > newfile

This User Gave Thanks to Franklin52 For This Post:
# 11  
Old 09-21-2011
Thank You franklin, that was informative Smilie

---------- Post updated 09-21-11 at 03:46 PM ---------- Previous update was 09-20-11 at 04:37 PM ----------

Have one more query about above solution..

We needed to print ,say "123" a line before occurrence of .sub. How is your code doing the same? Which part of the awk line means "print before .sub".
I need to understand the logic behind it.

Also, how will it change. if say i have to print the same "123" after the ".sub"
I need to understand the logic behind it.

Thanks
# 12  
Old 09-21-2011
Quote:
Originally Posted by animesharma
Thank You franklin, that was informative Smilie

---------- Post updated 09-21-11 at 03:46 PM ---------- Previous update was 09-20-11 at 04:37 PM ----------

Have one more query about above solution..

We needed to print ,say "123" a line before occurrence of .sub. How is your code doing the same? Which part of the awk line means "print before .sub".
I need to understand the logic behind it.

Also, how will it change. if say i have to print the same "123" after the ".sub"
I need to understand the logic behind it.

Thanks
Print "123" a line before ".sub":
Code:
awk -v var="123" '/^.sub/ && c++==0{print var}1' file > newfile

Print "123" a line after ".sub":
Code:
awk -v var="123" '/^.sub/ && c++==0{print $0 RS var; next}1' file > newfile

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