awk or sed to add field in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed to add field in a text file
# 1  
Old 12-31-2009
awk or sed to add field in a text file

Hi there, I have a csv file with some columns comma sepated like this :

Code:
4502-17,PETER,ITA2,LEGUE,92,ME - HALF,23/05/10 15:00
4502-18,CARL,ITA2,LEGUE,96,ME - HALF,20/01/09 14:00
4502-19,OTTO,ITA2,LEGUE,97,ME - MARY,23/05/10 15:00

As you can see the column n. 7 is a timestamp column, I need to estract just the year from column n. 7 adding a new column ( number 8 ) to the csv file so that the final result must the the following :

Code:
4502-17,PETER,ITA2,LEGUE,92,ME - HALF,23/05/10 15:00,2010
4502-18,CARL,ITA2,LEGUE,96,ME - HALF,20/01/09 14:00,2009
4502-19,OTTO,ITA2,LEGUE,97,ME - MARY,23/05/10 15:00,2010

I think the right solution is awk but my knowledge of awk if vary poor.
Thank you in adeance and happy new year to everybody.
# 2  
Old 12-31-2009
Try:
Code:
sed 's|/\(..\)\( ..:..\)|/\1\2,20\1|' infile

or:
Code:
awk -F, '{split($7,d,/[\/ ]/); $8="20"d[3]}1' OFS=',' infile


Last edited by Scrutinizer; 12-31-2009 at 12:29 PM..
# 3  
Old 12-31-2009
Or:
Code:
awk -F"[ /]" '{print $0 ",20" $(NF-1)}' file

# 4  
Old 01-03-2010
Code:
nawk -F"[, ]" '{split($9,arr,"[/]");printf("%s,20%s\n",$0,arr[3])}'

# 5  
Old 01-04-2010
Code:
 sed 's/\([0-9][0-9]\) \(.*\)/\1 \2,20\1/' urfile

# 6  
Old 01-04-2010
Another question about awk sed or tr

Thank you guys for your support all solutions are working perfectly.
In case I need a further modification of my original file splitting the last column from :


Code:
4502-1,4502,PETER,ITA1,LEGUE,92,ME HALF,02/06/10 15:00

Into

Code:
4502-1,4502,PETER,ITA1,LEGUE,92,ME HALF,02/06/10,15:00

How I can do it with awk, sed or tr ?
Last colums is a mysql timestamp filed
Thanks in advance for your kind reply.
# 7  
Old 01-04-2010
Quote:
Originally Posted by capnino
Thank you guys for your support all solutions are working perfectly.
In case I need a further modification of my original file splitting the last column from :


Code:
4502-1,4502,PETER,ITA1,LEGUE,92,ME HALF,02/06/10 15:00

Into

Code:
4502-1,4502,PETER,ITA1,LEGUE,92,ME HALF,02/06/10,15:00

How I can do it with awk, sed or tr ?
Last colums is a mysql timestamp filed
Thanks in advance for your kind reply.
Try this:
Code:
awk -F, '{sub(" ",",",$NF)}1' OFS="," file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to add field to diffrent file using shellscript? or awk

hi, would you help me? i have file total.csv "a","e23","f" "b,"34d","g" "c","45f","f" "d","45s","f" count.csv 3 i do this : paste -d',",' total.csv count.csv but the result like this: "a,"e23","f" 3 "b,"34d","g" (1 Reply)
Discussion started by: kivale
1 Replies

2. Shell Programming and Scripting

Using awk to add length of matching characters between field in file

The awk below produces the current output, which will add +1 to $3. However, I am trying to add the length of the matching characters between $5 and $6 to $3. I have tried using sub as a variable to store the length but am not able to do so correctly. I added comments to each line and the... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to add text to matching pattern in field

In the awk I am trying to add :p.=? to the end of each $9 that matches the pattern NM_. The below executes andis close but I can not seem to figure out why the :p.=? repeats in the split as in the green in the current output. I have added comments as well. Thank you :). file ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

6. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

7. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

8. Shell Programming and Scripting

sed add after line x new text from file

I've been playing with sed, trying to get it to insert the contents of somefile.txt after line 13 on anotherfile.txt. I tried searching for a line with regex and attempting to insert something on the next line with: find ./anotherfile.txt -type f -exec sed -i -e '/^dog/cat/' {} \; but it... (2 Replies)
Discussion started by: unclecameron
2 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. Shell Programming and Scripting

How to add text to a field within a csv file

I have a csv file which has three columns mem no. name surname 1234 John Smith 12345 John Doe I want to change the mem no. to add TF to the mem no. field i.e. mem no. name surname 1234TF John Smith 12345TF John Doe How do you do this for all records in the file? (3 Replies)
Discussion started by: Pablo_beezo
3 Replies
Login or Register to Ask a Question