Adding text into file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding text into file
# 1  
Old 03-10-2010
Adding text into file

Hello,

I after some assistance please. Below is a file I need to read and for each line write an output.

My input file looks like this

2 20008 2003-08-26
2 20032 2003-08-26
2 20041 2003-08-26
2 20037 2003-08-26
2 20050 2003-08-26
6 20 2005-04-25
6 10 2005-04-25
2 767 2007-06-04
2 768 2007-06-04
2 1744 2009-09-17

I know that column 1 = customer ID, column 2 = agent ID and column 3 = Date. So what I need is for each line write this

cust=[column1] agent=[column2] s_date=[column3] into a new file. The third column needs the date formatting as YYMMDD.

There is probably a real easy solution for this but at the moment I am stuck so any help would be greatly appreciated.

Thanks,
G
# 2  
Old 03-10-2010
Do you mean like this...
Code:
awk '{gsub("-","",$3);print "Cust="$1,"\tAgent="$2"\tDate="substr($3,3,6)}' infile

# 3  
Old 03-10-2010
That bombs out buddy

blade225Smilierms> awk '{gsub("-","",$3);print "Cust="$1," Agent="$2" Date="substr($3,3,5)}' agtskill.out
awk: syntax error near line 1
awk: illegal statement near line 1
# 4  
Old 03-10-2010
Quote:
Originally Posted by giles.cardew
That bombs out buddy

blade225Smilierms> awk '{gsub("-","",$3);print "Cust="$1," Agent="$2" Date="substr($3,3,5)}' agtskill.out
awk: syntax error near line 1
awk: illegal statement near line 1
Use nawk instead of awk
# 5  
Old 03-10-2010
Dude, I owe you a beer.

Thanks very much
# 6  
Old 03-10-2010
Code:
$
$ cat -n f3
     1  2 20008 2003-08-26
     2  2 20032 2003-08-26
     3  2 20041 2003-08-26
     4  2 20037 2003-08-26
     5  2 20050 2003-08-26
     6  6 20 2005-04-25
     7  6 10 2005-04-25
     8  2 767 2007-06-04
     9  2 768 2007-06-04
    10  2 1744 2009-09-17
$
$ perl -lne 'split/[- ]/ and print "cust=[$_[0]]\tagent=[$_[1]]\ts_date=[",substr($_[2],2),"$_[3]$_[4]]"' f3
cust=[2]        agent=[20008]   s_date=[030826]
cust=[2]        agent=[20032]   s_date=[030826]
cust=[2]        agent=[20041]   s_date=[030826]
cust=[2]        agent=[20037]   s_date=[030826]
cust=[2]        agent=[20050]   s_date=[030826]
cust=[6]        agent=[20]      s_date=[050425]
cust=[6]        agent=[10]      s_date=[050425]
cust=[2]        agent=[767]     s_date=[070604]
cust=[2]        agent=[768]     s_date=[070604]
cust=[2]        agent=[1744]    s_date=[090917]
$
$

tyler_durden

And another:

Code:
$
$ perl -lne '/(\d+) (\d+) \d\d(\d\d)-(\d\d)-(\d\d)/ and print "cust=[$1]\tagent=[$2]\ts_date=[$3$4$5]"' f3
cust=[2]        agent=[20008]   s_date=[030826]
cust=[2]        agent=[20032]   s_date=[030826]
cust=[2]        agent=[20041]   s_date=[030826]
cust=[2]        agent=[20037]   s_date=[030826]
cust=[2]        agent=[20050]   s_date=[030826]
cust=[6]        agent=[20]      s_date=[050425]
cust=[6]        agent=[10]      s_date=[050425]
cust=[2]        agent=[767]     s_date=[070604]
cust=[2]        agent=[768]     s_date=[070604]
cust=[2]        agent=[1744]    s_date=[090917]
$
$

# 7  
Old 03-11-2010
Code:
awk -F "[ -]" '{printf "cust=[%s]\tagent=[%s]\ta_date=[%s%s%s]\n",$1,$2,substr($3,3,2),$4,$5}' urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

About adding users from a text file using bash

hello, I try to add users from a text file with this form: username:groupename:homedir first i extract data which is separated by ":" then i use useradd to add users/groups. but,,, my code doesn't works : #!/bin/bash echo "give me a text file: " read dir # control if... (2 Replies)
Discussion started by: ref012
2 Replies

2. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

3. Shell Programming and Scripting

adding a line to a text file

I have a tab delimited text file, id name distance 1 3325167 0.334561754018 2 3290488 0.389444269458 3 3288794 0.392312701782 4 3347602 0.392532202097 5 3295355 0.394394169485 I need to add a line after the header line. The first and third field of... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

5. Shell Programming and Scripting

Adding Text To Middle Of File

Hi I am trying to write a bash script to add a line of text to the middle of a file. The way I worked it out was to calculate the number of lines and divide by 2. The file I am using is called newfile and it just contains lines of data. The new file I have created I have called midfile and... (7 Replies)
Discussion started by: BundBash
7 Replies

6. Shell Programming and Scripting

adding text to a file

Hi All I am making a script in which.I have a problem i try to discribe. File1 content need to be append in file 2 but such as if content already exist then dont add. File1 IS Like this myname yourname hername hisname File2 %AddNameHere myname yourname hername hisname (3 Replies)
Discussion started by: aliahsan81
3 Replies

7. Shell Programming and Scripting

Adding a delimiter to a text file

Im writing a KSH script to read a simple text file and add a delimiter. Ive written the following script but it runs very slow. I initially used the cut command to substring the input record then switched to this version using awk to substring... both run too slow. Any ideas how to make this more... (2 Replies)
Discussion started by: lock
2 Replies

8. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

9. Shell Programming and Scripting

adding text to a file between lines

Suppose content of my first file: first line second line third line How can i insert text between "first line" & "second Iline" Any help?????/ (7 Replies)
Discussion started by: bishweshwar
7 Replies

10. Shell Programming and Scripting

Adding Text To each line of a file

How would I add text to the beginning of each line in a text file in a script right after the file is created from another text file. (4 Replies)
Discussion started by: cubs0729
4 Replies
Login or Register to Ask a Question