How to append value at first line of CSV file using shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to append value at first line of CSV file using shell script?
# 1  
Old 11-26-2009
How to append value at first line of CSV file using shell script?

I have an issue where I need to append a value at the last of the csv, I have created a shell script and it is appending the columns at the last but it is appending at all lines, and my requirement is specific to just append at the 1st line.

Have a look and suggest,
# 2  
Old 11-26-2009
Quote:
Originally Posted by anujrichhariya
I have an issue where I need to append a value at the last of the csv, I have created a shell script and it is appending the columns at the last but it is appending at all lines, and my requirement is specific to just append at the 1st line.

Have a look and suggest,
Please post a sample input & output
# 3  
Old 11-26-2009
Hi Dennis,
Thanks for ur prompt reply.

Input file like:
AUS,NZM,USA
SRI,IND
SA,BD,SNG

Appended file should have values like:
AUS,NZM,USA,1,2
SRI,IND
SA,BD,SNG

Rgds
Anuj

Last edited by anujrichhariya; 11-26-2009 at 07:57 AM..
# 4  
Old 11-26-2009
Code:
num=1
while read -r line
do
    case "$num" in
        1) echo "${line},1,2";;
        *) echo ${line};;
    esac
    num=$(( num+1 ))
done < "file"

# 5  
Old 11-26-2009
I have to use "sed" pertaining to the requirement:
command currnetly used is "cat $1 | sed 's/$'"/,`echo \$2`,`echo \$3`/" > temp.csv" but it is appending the value in all lines of csv....like
AUS,NZM,USA,1,2
SRI,IND,1,2
SA,BD,SNG,1,2

Last edited by anujrichhariya; 11-26-2009 at 07:56 AM.. Reason: seplling mistake
# 6  
Old 11-26-2009
Code:
$ awk 'NR==1 {$0=$0",1,2"}1' input.txt
AUS,NZM,USA,1,2
SRI,IND
SA,BD,SNG

# 7  
Old 11-26-2009
Try:

Code:
awk 'NR==1 {  print $0",1,2"; } NR>1' file



---------- Post updated at 17:36 ---------- Previous update was at 17:29 ----------

Using sed:

Code:
sed '1s/.*/&,1,2/' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Shell Programming and Scripting

Need shell script to append double quotes for each column in a file

Hi Experts, I am beginner to the shell scripting, My requirement is to append double quotes for each column in a file if double quotes does not exist. Example: "abc"|123|"gh-ch"|23.067 Use code tags, thanks. (10 Replies)
Discussion started by: spidy
10 Replies

3. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

4. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. Shell Programming and Scripting

Need perl or shell script to sort vertical lines to horizontal line in csv format

Need perl or shell script to sort vertical lines to horizontal line in csv format My file like below ------------------------- ================================================================================ PATH PINKY1000#I1-1-ZENTA1000-2#I7-1-ASON-SBR-UP-943113845 ... (4 Replies)
Discussion started by: sreedhargouda.h
4 Replies

6. Shell Programming and Scripting

Remove line breaks in csv file using shell script

Hi All, I've a csv file in which the record is getting break into 1 line or more than one line. I want to combine those splits into one line and remove the unwanted character existing in the record i.e. double quote symbol ("). The line gets break only when the record contains double... (4 Replies)
Discussion started by: rajak.net
4 Replies

7. Shell Programming and Scripting

append dates going forward from today to certain line in shell script

Hi there, I have a requirement to append dates going forward to a certain line in a file. I'm not sure of how to go about this. Any help will be greatly appreciated. Thanks Slyesco:wall: (2 Replies)
Discussion started by: Slyesco
2 Replies

8. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

9. Shell Programming and Scripting

I want to append data to same .csv file.

I have a script which has to be scheduled to run 3 times a day. My script picks the required fields from logfile and stores the data in a.csv file. Sample data. my logfile contain: 0097A,0374D,100903,1519,00000606191 0097A,C88RA,100903,0724,00000606105 So the output of first execution... (3 Replies)
Discussion started by: shrima.pratima
3 Replies

10. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies
Login or Register to Ask a Question