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
# 8  
Old 01-06-2010
I corrected the first one (you can use it if you want to print the filename once per input file).
And yet another version:

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

# 9  
Old 01-06-2010
By Sed
Code:
sed -n '/^ABK.\{120\}ZBK/p' urfile

# 10  
Old 01-07-2010
There is a bug in my code: the index function will return the string position only for the first match, so if there is a match before the specific position the record will not be printed even if it contains the required data.

So, the OP should use the substr function.


Code:
... && substr($0,123,3) == "ZBK"


And the bits are for Jim Smilie
# 11  
Old 01-07-2010
Thanks for your reaction radoulov. As a matter of fact I saw this morning in one of our production servers that yesterday's command only worked in some files.

I'm using substr insted of index in your awk command and for now it seems it's working properly, I'll come back to you if I have other problem.

Many thanks for your help!
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