Help in adding a data after a particular line of data in a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in adding a data after a particular line of data in a file.
# 1  
Old 12-16-2011
Help in adding a data after a particular line of data in a file.

Hi.. I'm into a bump after trying to solve this prob.. i've a file with contents like below.
Code:
 <blankline>
'pgmId'             : 'UNIX',
'pgmData'          : 'textfile',
'author'             : 'admin',
.......

Now i'm trying to insert a new data after pgmId. so the final output will be like:
Code:
<blankline>
'pgmId'             : 'UNIX',
'moduleId'         : 'DEFAULT',
'pgmData'          : 'textfile',
'author'             : 'admin',
.......

I tried to fix this with a sed command like:
Code:
sed  "/pgmId/{G;}/" ./tempFile  | sed -e "s/^$/\     'moduleId\' \: \'DEFAULT\'\,/1">>./tempFile1

But this command is replacing all the blank lines with moduleId.

could anyone help me out?
thanks in advance.

Arjun

Last edited by Scott; 12-16-2011 at 05:30 AM.. Reason: Please use code tags
# 2  
Old 12-16-2011
Code:
 sed '/pgmId/a\
new_data' filename

# 3  
Old 12-16-2011
In nawk ..
Code:
$ echo $newline
'moduleId' : 'DEFAULT',
$
$ nawk -v var="$newline" '/.pgmId/{print $0"\n"var}!/.pgmId/' infile
 
'pgmId' : 'UNIX',
'moduleId' : 'DEFAULT',
'pgmData' : 'textfile',
'author' : 'admin',

# 4  
Old 12-16-2011
Hi,

A quick search in this forum for "sed insert line after text" led to this https://www.unix.com/shell-programmin...ding-text.html

Code:
$ echo "'pgmId' : 'UNIX',
'pgmData' : 'textfile',
'author' : 'admin'," | sed "/pgmId/ a\'moduleId':'DEFAULT'"

'pgmId' : 'UNIX',
'moduleId':'DEFAULT'
'pgmData' : 'textfile',
'author' : 'admin',

HTH
# 5  
Old 12-16-2011
Sed variant..
Code:
sed 's/pgmId.*$/&\nnew_data/' inputfile

# 6  
Old 12-16-2011
Hi michaelrozar17 Image
Registered User



while using this
Code:
sed 's/pgmId.*$/&\nnew_data/' inputfile

i'm getting the following error:
Code:
 sed: Function /pgmId/ a\'moduleId':'DEFAULT' cannot be parsed.

---------- Post updated at 02:05 PM ---------- Previous update was at 01:46 PM ----------

for all the answers given above, i'm able to add just moduleId default..

I wanted to add the line as
Code:
'moduleId'   : 'DEFAULT',

the error i'm getting is that the above said line cannot be parsed

Last edited by Scott; 12-16-2011 at 05:32 AM.. Reason: Code tags, please...
# 7  
Old 12-16-2011
Ok..try as
Code:
sed "s/pgmId.*$/&\n'moduleId':'DEFAULT',/" inputfile


Last edited by michaelrozar17; 12-16-2011 at 05:53 AM.. Reason: missed the comma..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX help adding data to end of a line with a variable

I am trying to add a date variable to the end of each line. This is what I have to start with cat ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv | grep ^',' | awk '{print $0}' >> ${DATAPATH}/badparticipant.csv This is what I tried $DATE is a variable I have defined. cat... (3 Replies)
Discussion started by: req62861
3 Replies

2. Shell Programming and Scripting

Adding a comment in a file next to data

Dear Gurus, I have a file which comes every day with set of data, as a part of processing i want to add a comment at the start of every line. e.g of file <PCL> 2E;"HCA";"COP Car A";"ODBS_CFG" 7C;"DD";"Doors Car D";"ODBS_CFG" 3D;"XA";"Auxiliary Car A";"ODBS_CFG" 3E;"XB";"Auxiliary... (12 Replies)
Discussion started by: guddu_12
12 Replies

3. Shell Programming and Scripting

Adding data from a file based on some condition

I collect data in a file in below format(Month Day Year Size) in RedHat Linux. Now i want to calculate the data size date wise. As i code shell script after long time, i forgot the features and syntax. Can anyone help me regard this please. Feb 8 2014 15 Feb 10 2014 32 Feb 10 2014 32 Feb 12... (2 Replies)
Discussion started by: makauser
2 Replies

4. UNIX for Dummies Questions & Answers

Mapping a data in a file and delete line in source file if data does not exist.

Hi Guys, Please help me with my problem here: I have a source file: 1212 23232 343434 ASAS1 4 3212 23232 343434 ASAS2 4 3234 23232 343434 QWQW1 4 1134 23232 343434 QWQW2 4 3212 23232 343434 QWQW3 4 and a mapping... (4 Replies)
Discussion started by: kokoro
4 Replies

5. Red Hat

Adding a data file

I need to add a data file, but my user account doesn't have privileges. I have connected to the oracle as sqlplus / as sysdba and it is telling me I'm connect to an idle instance. How can I connect to my database and create a data file if I don't know the passwords of any of the account that have... (1 Reply)
Discussion started by: FaSho76
1 Replies

6. Shell Programming and Scripting

Adding the data before file extension

HI , I have a file with multiple lines like below, I need to check on column starting with #prototype if it has the .Number.txt extension do nothing else add 1.txt extension. Source data Jdbc_app_data :- sample data #prototype= sample data from test.1.txt application_id=122135... (4 Replies)
Discussion started by: gaur.deepti
4 Replies

7. Shell Programming and Scripting

Adding data in a file on same line

Hi, I have one file a.txt ,the contents of the file is A B C D E F and I have another file b.txt, the contents of the file is 1 2 3 4 5 6 now when I am using this command cat a.txt b.txt > c.txtI am getting the output as A B C D E F 1 2 3 4 5 6 but i need the output... (2 Replies)
Discussion started by: prarat
2 Replies

8. Shell Programming and Scripting

adding the data at a specified location in a file....

Hi all, I m new to shell programming..Can anyone please guide me how to insert data at a specified location in the file.. I have a configuration file..I want to add data to it through script..I am able to do it...I get that data written at end of my configuration file..I want data to be placed at... (3 Replies)
Discussion started by: divya_flora
3 Replies

9. Shell Programming and Scripting

Need Help for Adding Three new columns in existing file from fatching data from file

not required this time (36 Replies)
Discussion started by: Sandeep_Malik
36 Replies

10. Shell Programming and Scripting

adding data behind a line

Hi, I need to search for the line with the particular group name and add the user into the group. The file format is the same as /etc/group The code i wrote is egrep "^$newGID" $group >/dev/null FS=":" oldData=awk -F: '{print $3}' newData= "$oldData,$newUser" sed -n $4/$newData $group... (1 Reply)
Discussion started by: cherrywinter
1 Replies
Login or Register to Ask a Question