Print entire line only if certain fixed character matches the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print entire line only if certain fixed character matches the string
# 1  
Old 10-09-2014
Print entire line only if certain fixed character matches the string

Hi All,

I have a file testarun.txt contains the below lines and i want to print the lines if the character positions 7-8 matches 01.

Code:
201401011111
201401022222
201402013333
201402024444
201403015555
201403026666
201404017777
201404028888
201405019999
201405020000

I am trying the below command
but im not getting the desired output of 5 lines which contains only 01 in the position 7-8.

Code:
awk '{ if (cut -c7-8 == 01) {printf $0} }' testarun.txt

Please help and this very emergency for me.

Thanks!

Last edited by Don Cragun; 10-09-2014 at 08:57 PM.. Reason: Add CODE tags.
# 2  
Old 10-09-2014
Code:
perl -lne 'print if substr($_, 6, 2) == 01;' testarun.txt

# 3  
Old 10-09-2014
using awk:

Code:
awk 'substr($0,7,2) == "01"' testarun.txt

# 4  
Old 10-13-2014
Using POG (Plain Ol'e Grep)

Code:
egrep '^.{6}01' testarun.txt

or, if you would like to stick to a even more limited version of grep:

Code:
grep '^......01' testarun.txt

I seem to be in one of two modes; if I have to initiate an interpreter I try and do everything in it; if I can get away with only using "simple" utilities I do.
# 5  
Old 10-13-2014
Or
Code:
grep '^.\{6\}01' testarun.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

2. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

3. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

4. Shell Programming and Scripting

Using regex's from file1, print line and line after matches in file2

Good day, I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after. file1: file2: Output: I can match a regex and print the line and line after awk '{lines = $0} /Macrosiphum_rosae/ {print lines ; print lines } ' ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

Print only matched string instead of entire line

Hi, I have a file whose lines are something like Tchampionspsq^@~^@^^^A^@^@^@^A^A^Aÿð^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?ð^@^@^@^@^@^@^@?ð^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A I need to extract all words matching T*psq from the file. Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

6. Shell Programming and Scripting

Print entire line based on value in a column

Friends, File1.txt abc|0|xyz 123|129|opq def|0|678 890|pqw|sdf How do I print the entire line where second column has value is 0? Expected Result: abc|0|xyz def|0|678 Thanks, Prashant ---------- Post updated at 02:14 PM ---------- Previous update was at 02:06 PM ---------- ... (1 Reply)
Discussion started by: ppat7046
1 Replies

7. Shell Programming and Scripting

Print the entire line if second field has value P

Friends, I have .txt file with 3 millions of rows. File1.txt ABC1|A|ABCD1|XYZ1 ABC2|P|ABCD2|XYZ2 ABC3|A|ABCD3|XYZ3 ABC4|P|ABCD4|XYZ4 If second field has value P then print the entire line. Thanks in advance for your help, Prashant (4 Replies)
Discussion started by: ppat7046
4 Replies

8. Shell Programming and Scripting

print entire line after if lookup

I hope this is a basic question. I have a file with a bunch of strings in each line (and the string number is variable). What I want to do is a simple if command and then print the entire line. something like awk '{if ($3=="yes") print $1,$2,$3,...$X }' infile > outfile Can someone... (1 Reply)
Discussion started by: dcfargo
1 Replies

9. Shell Programming and Scripting

print a file with one column having fixed character length

Hi guys, I have tried to find a solution for this problem but couln't. If anyone of you have an Idea do help me. INPUT_FILE with three columns shown to be separated by - sign A5BNK723NVI - 1 - 294 A7QZM0VIT - 251 - 537 A7NU3411V - 245 - 527 I want an output file in which First column... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

10. Shell Programming and Scripting

Replace Entire line if any part matches regexp

Hey guys, I have a file that I've slowly been awking, seding, and greping for data entry. I am down to pull the addresses out to insert them into an excel file. Each address is a few lines, but i want to put a semicolon delimiter in between each address so I can export the text file into excel and... (6 Replies)
Discussion started by: Cocoabean
6 Replies
Login or Register to Ask a Question