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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers UNIX help adding data to end of a line with a variable
# 1  
Old 04-04-2017
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

Code:
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.

Code:
cat ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv | grep ^',' | awk '{print $0 $DATE}' >> ${DATAPATH}/badparticipant.csv

Any suggestions?


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-04-2017 at 05:32 PM.. Reason: Added CODE tags.
# 2  
Old 04-04-2017
Welcome to the forum.

Thanks for sharing your approach. Some comments on it:

  • Please supply minimum context info like OS and shell version, sample input, and desired output; or even more if applicable and helpful.
  • cat is not needed - grep or awk or many other commands do open and read files by themselves.
  • awk has great pattern matching capabilities - no grep piping into it needed.
  • shell variables ($DATE) are NEVER expanded within single quotes - use different mechanisms.
  • It's good habit to deploy the tool's of choice (awk) mechanisms - read the man page on the -v option for parameter passing.

Not sure what the ^',' pattern stands for and how it is being interpreted, try this modified version of your command pipe, if it comes close to what you need:
Code:
awk -v DT="$DATE" '/^,/ {print $0, DT}' ${DATAPATH}/Participate_Stream${STREAMDATE}.dryak1.csv >> ${DATAPATH}/badparticipant.csv

# 3  
Old 04-05-2017
Reply

The grep ^',' is looking for a comma in the first position of the file I have. If that is true then I want to Print the record and add the DATE variable I have created.

It would look something like this.

Code:
 ,John Smith,somewhere,blue,outside,20170405


Last edited by rbatte1; 04-05-2017 at 11:12 AM.. Reason: Added CODE tags & ICODE tags
# 4  
Old 04-06-2017
Thanks

That got me very close. I had to tweak it a bit because of my file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

2. Shell Programming and Scripting

Adding semicolon at the end of each line

Hi, I have a script which I need to change. I want to add a semicolon at the end of each line where the line starts with "grant" for e.g. create table(.... ); grant select on TABL1 to USER1 grant select on TABL1 to USER2should become create table(.... ); grant select on TABL1 to... (3 Replies)
Discussion started by: pparthiv
3 Replies

3. 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

4. Shell Programming and Scripting

Script adding ^M to end of line

I trying to make a simple script to get info from remote servers my problem is the output of this line- SERVER_NAME=`ssh -t $USER@$REMOTESERVER 'hostname'`the output is linux1^M I would like to remove the ^M where is my error? Many Thanks -Steve (1 Reply)
Discussion started by: shoodlum
1 Replies

5. UNIX for Dummies Questions & Answers

Adding comma at the end of every line

Hi all, I have this sample file (actual file is larger) and i need to add comma at the end of every line. 1234 4335 232345 1212 3535 Output 1234, 4335, 232345, 1212, 3535, TIA - jak (2 Replies)
Discussion started by: jakSun8
2 Replies

6. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

7. Shell Programming and Scripting

Adding lines at end of a line

This is what I want to do. I want to write a script that reads each line (of the highlighted file below) and add a specific number of blank lines (sometime 2, 3 or 5 lines) at the end of each line while copying that line. For example, here is the input. The sky is blue. I like to eat. I like... (19 Replies)
Discussion started by: Ernst
19 Replies

8. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies

9. Shell Programming and Scripting

adding semicolumn at end of line

Hi , I need to add semicolumn at the end of each line in a file. can any one help me in this? Thanks in advance (2 Replies)
Discussion started by: scorpio
2 Replies

10. Shell Programming and Scripting

Adding new line at the end of file

Hi I have few files. For some files the cursor is at the end of last line. For other files, cursor is at the new line at the end. I want to bring the cursor down to next line for the files that are having cursor at the end of last line In otherwords, I want to introduce a blank line at the... (5 Replies)
Discussion started by: somesh_p
5 Replies
Login or Register to Ask a Question