AWK: list files with 1rst col=N and char position 123=N


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: list files with 1rst col=N and char position 123=N
# 1  
Old 01-06-2010
AWK: list files with 1rst col=N and char position 123=N

I need to list all files where 1rst column=ABK and char position 123 to 125=ZBK:

For the first part I can I can do a awk '{$1="ABK";print}' file and for the second a cut -c123-125 file | grep ZBK but this would only work partially..

How can I do this with only one awk command ?

Thanks in advance,
# 2  
Old 01-06-2010
Code:
awk ' $1="ABK" && substr($0,123,1)=="N"  ' file > newfile

# 3  
Old 01-06-2010
Code:
awk '$1 == "ABK" && index($0, "ZBK") == 123' infile

# 4  
Old 01-06-2010
That worked perfectly as I expected thanks a lot for both answers!

I have just one more question: Since I have hundred of files instead of doing your awk command file, I would like to run it to all files (awk command *) and therefore I would also need to list/print all files containing the result of my awk command.

May you help me one more time ?

Thanks once again
# 5  
Old 01-06-2010
hi,
I am the new member, I want to place a question on shell scripting, can you please tell me to options to place my post.

Thanks
# 6  
Old 01-06-2010
Quote:
Originally Posted by cabrao
That worked perfectly as I expected thanks a lot for both answers!

I have just one more question: Since I have hundred of files instead of doing your awk command file, I would like to run it to all files (awk command *) and therefore I would also need to list/print all files containing the result of my awk command.
[...]
Use this:

Code:
awk 'FNR == 1 { c = 0 }
$1 == "ABK" && index($0, "ZBK") == 123 {
  c++ || $0 = FILENAME RS $0; print 
  }' *

Or this:

Code:
awk '$1 == "ABK" && index($0, "ZBK") == 123 { 
  print FILENAME, $0 
  }' *



---------- Post updated at 07:25 PM ---------- Previous update was at 07:20 PM ----------

Quote:
Originally Posted by bcb
hi,
I am the new member, I want to place a question on shell scripting, can you please tell me to options to place my post.

Thanks
Welcome to UNIX.COM!
Go to the Shell Programming and Scripting forum and use the new thread button in the upper left corner.

... and don't forget to read the forum rules.

Last edited by radoulov; 01-06-2010 at 03:14 PM.. Reason: corrected again
# 7  
Old 01-06-2010
I have nothing more to say..., just a BIG BIG THANK YOU radoulov!

Thanks for your great help Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command 2nd col remove the '-' value

Hi, i was tried using the awk command for replacing '-' in the second column. but the below command replacing the entire file. cat 1.txt |awk '{gsub(/-/,"")}1' Input file 1,2,3,-4,5,6 1,-2,3,4,5,-6 1,2,3,4,5,6 1,-2,3,4,-5,6 Output file 1,2,3,-4,5,6 1,2,3,4,5,-6 1,2,3,4,5,6... (3 Replies)
Discussion started by: onesuri
3 Replies

2. Shell Programming and Scripting

Merge files by col value

Hi, Please help, this is quite complex, I dont know how to start. The original input files are 10mb in size each. I have multiple files and I want to merge them in the following way. Every file has 4 columns. Col1 and col2 are fixed with respect to each other. In the example value A... (2 Replies)
Discussion started by: alpesh
2 Replies

3. Shell Programming and Scripting

Modifying col values based on another col

Hi, Please help with this. I have several excel files (with and .xlsx format) with 10-15 columns each. They all have the same type of data but the columns are not ordered in the same way. Here is a 3 column example. What I want to do add the alphabet from column 2 to column 3, provided... (9 Replies)
Discussion started by: newbie83
9 Replies

4. Shell Programming and Scripting

Printing from col x to end of line, except last col

Hello, I have some tab delimited data and I need to move the last col. I could hard code it, awk '{ print $1,$NF,$2,$3,$4,etc }' infile > outfile but it would be nice to know the syntax to print a range cols. I know in cut you can do, cut -f 1,4-8,11- to print fields 1,... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

5. Shell Programming and Scripting

Insert carriage return on the 10th char position of each line

Hi experts, Need your help on how to insert carriage return after the 10th char position of each line in a file and then add two blank spaces after the carriage return. Example: >cat test.txt testingline dummystring samplesample teststringline Expected output should be.. ... (2 Replies)
Discussion started by: brichigo
2 Replies

6. Shell Programming and Scripting

awk - getting uniq count on multiple col

Hi My file have 7 column, FIle is pipe delimed Col1|Col2|col3|Col4|col5|Col6|Col7 I want to find out uniq record count on col3, col4 and col2 ( same order) how can I achieve it. ex 1|3|A|V|C|1|1 1|3|A|V|C|1|1 1|4|A|V|C|1|1 Output should be FREQ|A|V|3|2 FREQ|A|V|4|1 Here... (5 Replies)
Discussion started by: sanranad
5 Replies

7. UNIX for Advanced & Expert Users

Print line based on highest value of col (B) and repetion of values in col (A)

Hello everyone, I am writing a script to process data from the ATP world tour. I have a file which contains: t=540 y=2011 r=1 p=N409 t=540 y=2011 r=2 p=N409 t=540 y=2011 r=3 p=N409 t=540 y=2011 r=4 p=N409 t=520 y=2011 r=1 p=N409 t=520 y=2011 r=2 p=N409 t=520 y=2011 r=3 p=N409 The... (4 Replies)
Discussion started by: imahmoud
4 Replies

8. Shell Programming and Scripting

Reading a file and replacing char by position

Hi I'm looking for a way to read a text file that may contain 1000 records or more and each of these records has 460 characters. I need to read each record, and add a string of characters starting at position 256 for each record. Any suggestions using UNIX shell scripting. (4 Replies)
Discussion started by: macastor
4 Replies

9. Shell Programming and Scripting

Remove text from n position to n position sed/awk

I want to remove text from nth position to nth position couple of times in same line my line is "hello is there anyone can help me with this question" I need like this ello is there anyone can help me with question 'h' is removed and 'this' removed from the line. I want to do this... (5 Replies)
Discussion started by: elamurugu
5 Replies

10. Shell Programming and Scripting

Awk to print distinct col values

Hi Guys... I am newbie to awk and would like a solution to probably one of the simple practical questions. I have a test file that goes as: 1,2,3,4,5,6 7,2,3,8,7,6 9,3,5,6,7,3 8,3,1,1,1,1 4,4,2,2,2,2 I would like to know how AWK can get me the distinct values say for eg: on col2... (22 Replies)
Discussion started by: anduzzi
22 Replies
Login or Register to Ask a Question