How to get lines started with matched strings using sed or grep for loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get lines started with matched strings using sed or grep for loop?
# 1  
Old 06-28-2009
Question How to get lines started with matched strings using sed or grep for loop?

I have a huge file and want to separate it into several subsets.
The file looks like:

C1 C2 C3 C4 ... (variable names)
1 ....
2 ....
3 ....
:
22 ....
23 ....

I want to separate the huge file using the column 1, which has numbers from 1 to 23 (but there are different amount of lines for each number).
I tried to use the loop looks like this:

@ i = 1
while ($i <= 23)
grep ^$i hugefile.txt > $i.txt
@ i ++
end

But there is one problem that for 1.txt, it will include all lines started with 1, 10, 11,..19

I tried to use
grep "\<$i\>" instead, but it doesn't work too. Since in the other columns might contain numbers 1 to 23, and will add into the wrong files.

I am thinking maybe I can awk all the columns if column 1 = $i?

Is there any suggestion for this situation? Thank you in advanceSmilie.
# 2  
Old 06-28-2009
Code:
while read first rest; do
    echo "$first $rest" >> $first.txt
done

Side effect: It changes all whitespace between first and second field into space.
# 3  
Old 06-28-2009
Previous ex. using read is better, but if you like to use grep then you must grep value+space. If you use grep to find lines using some key, use always with field delimeter. This works using ksh, bash, posix-sh, ...
Code:
i=1
while ((i <= 23))
do
  grep "^$i " hugefile.txt > $i.txt
 ((i+=1))
end

# 4  
Old 06-28-2009
Try this:

Code:
awk '{print > $1".txt"}' file

Regards
# 5  
Old 06-29-2009
Code:
nawk '{file=sprintf("%s.txt",$1);print $0 >> file}' yourfile

# 6  
Old 06-29-2009
Thank you so much! All posts are really helpful, it works using read, grep, awk and nwak~ The 'grep value+space' is really smart, it solved so many problems~ Thank you guys again Smilie
# 7  
Old 07-13-2009
I have somewhat similar problem.
Hello,



I have a file with junk characters at the beginning of the file. Pls advice how could i bypass these junk characters. Sample file is given below.I want to extract the data starting from CREATE keyword to till end of the file.



SQL08029QDB2/AIX6400AIX 64BIT@MADMS012009-07-13-00.05.04.732394PSQLASC_ANNUALIZEDRATEOFRETURNACTIVITY"SQL080226170053405MADMS01I MADMS01LDID074"SYSIBM","SYSFUN","SYSPROC","LDID074"%P5270293MADMS01*SPACE RESERVED FOR FUTURE*ÉPVM 00SQL08029QDB2/AIX64ÿÿÿÿSQL090513152703000
¸°ñÿÿÿÅÿÿÿÿñÿÿÿÿÁÿÿÿÿ ñÿÿÿÿ¨ñÿÿÿÿ°ñÿÿÿÿ¸ÁFHÿÿÿÿÁ$&GÀkÀÀðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿKÿÿÿÿÿÿÿÿCREATE PROCEDURE"MADMS01I"."ASC_ANNUALIZEDRATEOFRETURNACTIVITY"
(IN "GUIDPOLICYGUID" VARCHAR(36),
IN "DPERIODSTARTDATE" TIME
END
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

2. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

3. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

4. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

5. Shell Programming and Scripting

Sed grep the first matched lines

Hi I have a myfile.txt contains the following: CONTEXT { AAAAA } ... CONTEXT { BBBBB } I want to extract the lines in between CONTEXT { ... }, one by one. Hence I wrote a command like the following, sed -n '/^CONTENT/,/^}/ { w a.txt }' myfile.txt The problem with this... (5 Replies)
Discussion started by: hezjing
5 Replies

6. Shell Programming and Scripting

Grep and delete lines except the lines with strings

Hi I am writing a script which should read a file and search for certain strings 'approved' or 'removed' and retain only those lines that contain the above strings. Ex: file name 'test' test: approved package waiting for approval package disapproved package removed package approved... (14 Replies)
Discussion started by: vj8436
14 Replies

7. Shell Programming and Scripting

print only matched string instead lines in grep

frnd, Is there any way to print only the string that matched the pattern instead printing the whole line? thanks in advance. (3 Replies)
Discussion started by: clx
3 Replies

8. Shell Programming and Scripting

sed replaces all matched strings!!!

hi sed -e '/<group>/!s/group\(.*\)/group\: files compat/' /etc/nsswitch.conf returns group: files compat netgroup: files compat How to prevent changing netgroup entry?? thank you (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

9. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question