awk to check begining of each row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to check begining of each row
# 1  
Old 06-23-2011
awk to check begining of each row

Hi,

I want to insert columns into a file. Some are standard (e.g. 1) and some are dependent on the contents on other columns. I am using the following code

In the below the first 1 (after $2), can be a 1 or 3 depending on what $1 starts with

e.g. if $1 is 405 then the third field in the output file is a 1
if $1 is anything other than 405 then the third field in the output file is a 3

Code:
awk -F'|' '
{if (NF == 11) {
 print $1","$2",""1"",""1"","$3","$4","$5","$6",""20"","$7",""1"","$8","$9","$10; }}' test1.csv > test2.csv

Thanks
Kieran
# 2  
Old 06-23-2011
Ok. What the problem is now?
# 3  
Old 06-23-2011
I don't understand the question?

I want to add into the awk statement a check to see what $1 begins with to decide whether to output a 1 or a 3 in column 3.
# 4  
Old 06-23-2011
Something like this: ?

Code:
 
/home/ravi/>cat input_file
1,a,b,c,d
450,A,B,C,D
/home/ravi/>awk -F"," '$1==450 {$3="1,"$3} $1!=450 {$3="3,"$3} 1' OFS="," input_file
1,a,3,b,c,d
450,A,1,B,C,D

# 5  
Old 06-23-2011
Code:
mute@geek:~/test$ cat input
1|2|3|4|5|6|7|8|9|10|11
405|2|3|4|5|6|7|8|9|10|11
1|2|3|4|5|6|7|8|9|10|11
33|2|3|4|5|6|7|8|9|10|11
11|2|3|4|5|6|7|8|9|10|11

Code:
mute@geek:~/test$ awk -v FS='|' 'NF == 11 { printf("%s,%s,%d,1,%s,%s,%s,%s,20,%s,1,%s,%s,%s\n", $1, $2, ($1 == 405) ? 3 : 1, $3, $4, $5, $6, $7, $8, $9, $10) }' input
1,2,1,1,3,4,5,6,20,7,1,8,9,10
405,2,3,1,3,4,5,6,20,7,1,8,9,10
1,2,1,1,3,4,5,6,20,7,1,8,9,10
33,2,1,1,3,4,5,6,20,7,1,8,9,10
11,2,1,1,3,4,5,6,20,7,1,8,9,10

# 6  
Old 06-23-2011
Thanks. that works if the column is 3 digits only. One issue

$1 is 15 digits in lenght and the 15 digit number is generally different in each row.
e.g. 405030001534176

but its only the 405 (or not 405) I need to use for the comparison.

I'm looking to check where the row begins with 405 or not

Thanks
# 7  
Old 06-23-2011
replace ($1 == 405) with ($1 ~ /^405/)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort by first row - awk

how can i sort the table based on first row? thanks in advance input name d b c a l l1 l2 l3 l4 l1 1 2 3 4 l2 2 2 2 1 l3 1 1 2 2ouput name a b c d l1 l4 ... (4 Replies)
Discussion started by: quincyjones
4 Replies

2. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

3. Shell Programming and Scripting

Parse tab delimited file, check condition and delete row

I am fairly new to programming and trying to resolve this problem. I have the file like this. CHROM POS REF ALT 10_sample.bam 11_sample.bam 12_sample.bam 13_sample.bam 14_sample.bam 15_sample.bam 16_sample.bam tg93 77 T C T T T T T tg93 79 ... (4 Replies)
Discussion started by: empyrean
4 Replies

4. Shell Programming and Scripting

awk get the row

somthing somthing A B C B F A B .... 1 2 3 4 5 6 7 .... 7 9 12 2 4 5 8 I want to get the row which is the same row as B. This this the 2 4 7 9 2 8 (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. Shell Programming and Scripting

Request to check:remove entries with duplicate numbers in first row

Hi I have a file 1 xyz 456 1 xyz 456 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 4 ghf 658 I want the output 1 xyz 456 2 abc 8459 3 gfd 657 4 ghf 658 (3 Replies)
Discussion started by: manigrover
3 Replies

6. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

7. Shell Programming and Scripting

Adjacent row and column check in Perl

HI, i am new to perl world. And i am trying to compress a file, as given below procedure. INPUT FILE: 1 1 2 1 ==> R1 2 1 3 1 ==> R2 3 1 4 1 ==> R3 OUTPUT FILE: 1 1 4 1 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

8. UNIX for Dummies Questions & Answers

How to check if there is only one row in the file?

How can I check if there is only one row in the file? If there are more than one rows, I need to send an email and if there is only one row, do not do anything. Thanks in anticipation (7 Replies)
Discussion started by: er_ashu
7 Replies

9. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

10. Shell Programming and Scripting

Value of previous row using awk

I would like to know value for previous row, in addition to current row. How would I will get value for previous row? How can I perform this with awk? (2 Replies)
Discussion started by: videsh77
2 Replies
Login or Register to Ask a Question