Add "1234" to specific field in second column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add "1234" to specific field in second column
# 1  
Old 12-31-2012
Add "1234" to specific field in second column

i have this file

Code:
073274753,0544901701,20101201,000316
038873722,69647455,20101130,235257
26213399,0545335767,20101201,000930
063330167,0566000101,20101201,000226
026773376,11966,20101130,234429,1194
075431120,0565900600,20101201,000428
75431120,0565900600,20101201,000538
038239110,0569224091,20101130,235616
035933611,0568580075,20101130,235939
025668601,0565651151,20101201,000046

i need if i have in first column any number doesn't start with zero i will add zero to it like this

Code:
75431120,0565900600,20101201,000538

Code:
075431120,0565900600,20101201,000538

also if i have in second column any number doesn't start with zero add "1234" to it
Code:
026773376,11966,20101130,234429,1194

Code:
026773376,123411966,20101130,234429,1194

and print all lines in the file "modificated and non modificated" all
# 2  
Old 12-31-2012
Code:
awk 'BEGIN{FS=OFS=","}
    $1 !~ /^0/{$1="0"$1}
    $2 !~ /^0/{$2="1234"$2}1' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 12-31-2012
Code:
awk 'BEGIN{FS=OFS=","}     $1 !~ /^0/{$1="0"$1}     $2 !~ /^0/{$2="1234"$2}1' file

could you explain why you add "1" ?
# 4  
Old 12-31-2012
awk

Quote:
Originally Posted by maxim42
Code:
awk 'BEGIN{FS=OFS=","}     $1 !~ /^0/{$1="0"$1}     $2 !~ /^0/{$2="1234"$2}1' file

could you explain why you add "1" ?
That will print all the lines. Implicitly call the below,

Code:
1{print;}

1 can be replaced by any positive integer.

Cheers,
RangaSmilie
# 5  
Old 12-31-2012
thats equivalent to print $0, means prints all the contents.

Code:
# awk 'BEGIN{FS=OFS=","}
    $1 !~ /^0/{$1="0"$1}
    $2 !~ /^0/{$2="1234"$2} {print $0}' file

073274753,0544901701,20101201,000316
038873722,123469647455,20101130,235257
026213399,0545335767,20101201,000930
063330167,0566000101,20101201,000226
026773376,123411966,20101130,234429,1194
075431120,0565900600,20101201,000428
075431120,0565900600,20101201,000538
038239110,0569224091,20101130,235616
035933611,0568580075,20101130,235939
025668601,0565651151,20101201,000046

This User Gave Thanks to sathyaonnuix For This Post:
# 6  
Old 12-31-2012
Quote:
Originally Posted by maxim42
Code:
awk 'BEGIN{FS=OFS=","}     $1 !~ /^0/{$1="0"$1}     $2 !~ /^0/{$2="1234"$2}1' file

could you explain why you add "1" ?
1 is same as we use print.

The same above you can also write with print.

Code:
awk 'BEGIN{FS=OFS=","}
    $1 !~ /^0/{$1="0"$1}
    $2 !~ /^0/{$2="1234"$2}{print}' file

# 7  
Old 12-31-2012
Quote:
Originally Posted by rangarasan
1 can be replaced by any positive integer.
1 may be replaced with any non-zero number, positive or negative.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Add " " to a field with awk

Hello, I would like to add " " in the last column of my file. For example, I have: "1";toto "2";tataI would like: "1";"toto" "2";"tata"I tried : awk 'BEGIN{FS=";"}$2="\""$2"\""' and the result was: "1";toto""If you have any ideas, I would appreciate your help, thanks you! Please use... (4 Replies)
Discussion started by: Nathalie10
4 Replies

3. Shell Programming and Scripting

Awk,sed : change every 2nd field ":" to "|"

Hi Experts, I have a string with colon delimited, want 2nd colon to be changed to a pipe. data: 101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3: I am trying with sed, but can change only 1 occurance: echo "101:8:43:4:72:14:41:69:85:3:137:4:3:0:4:0:9:3:0:3:12:3:" | sed 's/:/|/2'... (5 Replies)
Discussion started by: rveri
5 Replies

4. UNIX for Advanced & Expert Users

Should I say "field 8" or "column 8" in this case?

I saw some recent posts where I thought the terms "field" and "column" were being misused. I work with data a lot, and have my opinions. I'm wondering if those opinions are correct. ***** Rows seem clear - I don't think there is any controversy about what a row is, either for database or text... (10 Replies)
Discussion started by: hanson44
10 Replies

5. Shell Programming and Scripting

Substituting comma "," for dot "." in a specific column when comma"," is a delimiter

Hi, I'm dealing with an issue and losing a lot of hours figuring out how i would solve this. I have an input file which looks like this: ('BLABLA +200-GRS','Serviço ','TarifaçãoServiço','wap.bla.us.0000000121',2985,0,55,' de conversão em escada','Dia','Domingos') ('BLABLA +200-GRR','Serviço... (6 Replies)
Discussion started by: poliver
6 Replies

6. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

9. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

10. UNIX for Dummies Questions & Answers

How to find all files containing "1234" in their inode number?

Hi, if there are 3 files with the following inode numbers: 012345 012346 012347 is there a way to find all those files containing "1234" as inode? i found out that i cant use any wildcards. or i just didnt put them in the right way. find . -inum ... ? thanks 4 help. devil (2 Replies)
Discussion started by: daredevil82m
2 Replies
Login or Register to Ask a Question