How to awk/sed/grep lines which contains a pattern at a given position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to awk/sed/grep lines which contains a pattern at a given position
# 1  
Old 10-29-2008
How to awk/sed/grep lines which contains a pattern at a given position

Dear friends

I am new to linux and was trying to split some files userwise in our linux server.

I have a data file of 156 continuous columns named ecscr final.

I want the script to redirect all the lines containing a pattern of 7 digits to separate files. I was using grep to do that, as below. I have written all available patterns to a file named usercode.

while read alpha
do
cat ecscrfinal | grep -e $alpha >> mkoff.$alpha
echo "Generating markoff data for $alpha"
done < /home/ftp/ecs/usercode


But I need only those lines which have the pattern occuring at 3rd column or 81st column to go to that file. In the above case if the pattern is found any wher in the line, it is redirected to that file which gives me a wrong result.

How sed/awk/grep can be used to match a pattern occuring only at a specified field.....?

The pattern should be matched only if it is occuring in 3rd position or 81st postition.

Please help me.....i am running against a deadline.........
# 2  
Old 10-29-2008
Hammer & Screwdriver The following may work for you

Code:
> cat file20
abcde1234567ffffffffggggggg
abcdezzzzzzzffffffffggggggg
abczzyyyyyyygggggggg1212121

> awk 'substr($0,6,7)<"AAAAAAA" || substr($0,21,7)<"AAAAAAA"' file20
abcde1234567ffffffffggggggg
abczzyyyyyyygggggggg1212121

It selects records that are numeric at either of two positions in the data record.
# 3  
Old 10-29-2008
i tried like this

while read alpha
do
awk 'substr($0,3,7)<"$alpha"||substr($0,81,7)<"$alpha"'<ecscrfinal >> mkoff.$alpha
awk 'substr($0,3,7)<"$alpha"||substr($0,81,7)<"$alpha"' <ecsdrfinal >> mkoff.$alpha
done < /home/ftp/ecs/usercode

which yielded no results

then like this, $alph without quotes;

while read alpha
do
awk 'substr($0,3,7)<$alpha||substr($0,81,7)<$alpha'<ecscrfinal >> mkoff.$alpha
awk 'substr($0,3,7)<$alpha||substr($0,81,7)<$alpha' <ecsdrfinal >> mkoff.$alpha
done < /home/ftp/ecs/usercode

In this case the entire file is copied to all the userwise split files

What I am doing wrong..... Cant I do substitution inside awk...??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

3. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

Sed/awk : to grep only required pattern disk

Hi Experts, Need help with the following: Desired output: Only want to get the output marked in green. The file: --- Physical volumes --- PV Name /dev/disk/disk4704 PV Status available Total PE 6399 Free PE ... (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

Getting lines before and until next pattern in file /awk, sed

Hi, I need to get specific parts in a large file. I need to: Get a line containing an IP address, and read from there to another line saying ***SNMP-END*** So, I have the start and the end well defined, but the problem is that apparently the awk command using the -F option doesn't work... (17 Replies)
Discussion started by: ocramas
17 Replies

6. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

7. UNIX for Dummies Questions & Answers

One liner pattern search with awk/sed/grep

I have an array containing bunch of characters. I have to check this array for specific character and if "Not Found than" use a goto statement to go to USAGE set options = (A B C D E F) @ i = 0 while ($i <= ${#options}) if ($options != "F" || $options != "D") then goto USAGE endif @... (1 Reply)
Discussion started by: dixits
1 Replies

8. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 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

Extracting pattern only with AWK | SED | GREP

We have the following statement working in CGYWIN, but when we move the program to Solaris 10 it fails. x=`echo "ABC196925XYZ" | grep -o --only-matching "\{6\}"` How can we use AWK or SED to extract only the number from the string? The following outputs the entire string. We only want... (5 Replies)
Discussion started by: James Clark
5 Replies
Login or Register to Ask a Question