Bash script appending data after comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script appending data after comma
# 1  
Old 01-23-2017
Bash script appending data after comma

Hi ,

i have a file say test with following contents.

Code:
[groups]

test_read =  apple,orange
write = grapes,mango

i need to add raspberry on all the fields under groups section like this.

Code:
[groups]

test_read =  apple,orange,raspberry
write = grapes,mango,raspberry

Kindly help
# 2  
Old 01-23-2017
Try (untested):
Code:
awk 'NF > 1 {$NF = $NF ",raspberry"} 1' file

This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-23-2017
Hi RudiC,

worked like a charm for the mentioned input. Thanks

can you please help me for this as well.

Modified Input

Code:
[groups]

test_read = apple,orange
write = grapes,mango
[TEST:/]
@test_read= apple,orange
@write= grapes,mango

rasperry needs to be added only in fields which is above this line [TEST:/]

Expected output

Code:
test_read = apple,orange,raspberry
write = grapes,mango,raspberry
[TEST:/]
@test_read= apple,orange
@write= grapes,mango


Kindly help
# 4  
Old 01-23-2017
Do you need the [groups] line in your output?

Try (untested)
Code:
awk '
!LOCK &&
NF > 1       {$NF = $NF ",raspberry"
             } 
/[TEST:\/]/  {LOCK = 1
             }
1' file

This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-23-2017
Thanks rudi

worked like a charm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with data appending to a file

Hi I have a file called text.txt contains x y z when i run a command i will get output like below x 20 z 30 i want to insert x, z value in text.txt file and should be like this x 20 y 0 z 30 can anyone help me please? (1 Reply)
Discussion started by: siva kumar
1 Replies

2. UNIX for Dummies Questions & Answers

appending a command to print a file in a comma delimited format

Hi everyone, i have a file that I had grep'd from something else lets call it file1.txt which consists variable files and lines due to different scenarios/inputs 1782 9182 fe35 ac67 how can I print this in this manner? 1782,9182,fe35,ac67 also if i had piped the new output... (2 Replies)
Discussion started by: prodigy06
2 Replies

3. Shell Programming and Scripting

appending data from similar files

I am familiar with scripting, but I am trying to see if there is an easy way to append files from similar files into one file. For example, if there is file1_20121201, file1_20121202, file1_20121203, file2_20121201, file2_20121202, file2_20121203 I want to be able to combine all the data from... (3 Replies)
Discussion started by: mrbean1975
3 Replies

4. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

5. Shell Programming and Scripting

Pull Data After Comma if 2 word before comma

Hi, I am trying to truncate word after comma in a file ONLY if there are already 2 words BEFORE comma. If there is one word or 3 or more words BEFORE comma, then I have to leave the data AS IS. See below for example. Input File : John Smith, Manager Smith, John Frank J F K... (2 Replies)
Discussion started by: msalam65
2 Replies

6. Shell Programming and Scripting

appending previous data.

I have on file abc.txt abc.txt: 20090807 Now I want to delete empty lines which has tap/whit spaces from abc.txt .and store the date value in the file into variable.some processs will update the this file with some date . if the process updtes thiis file with empty string , write the... (3 Replies)
Discussion started by: Gopal_Engg
3 Replies

7. Shell Programming and Scripting

Appending data into a variable

Hi, I would like to know if it's possible to append data into a variable, rather than into a file. Although I can write information into a temporary file in /tmp, I'd rather if possible write into a variable, as I don't like the idea that should my script fail, I'll be polluting the server with... (5 Replies)
Discussion started by: michaeltravisuk
5 Replies

8. Shell Programming and Scripting

appending data to file

Hi. I wrote a very simple script and it doesn't work :( It is supposed to go to a certain directory, execute some command and append the output to the file "expo.dat" what it does is that it writes to the file only one entery. I dont know if Im using the write synthax for "append". Here is... (3 Replies)
Discussion started by: Enigma08
3 Replies

9. UNIX for Dummies Questions & Answers

converting a tabular format data to comma seperated data in KSH

Hi, Could anyone help me in changing a tabular format output to comma seperated file pls in K-sh. Its very urgent. E.g : username empid ------------------------ sri 123 to username,empid sri,123 Thanks, Hema:confused: (2 Replies)
Discussion started by: Hemamalini
2 Replies

10. UNIX for Dummies Questions & Answers

help on appending data to existing data

I need to know how to record the hostname, date/time and all of the process and send it all to one file. I know that the commands I need are hostname, date and ps but I don't know how to do them all and send them all to the same file. Please help! (1 Reply)
Discussion started by: precious51980
1 Replies
Login or Register to Ask a Question