The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep and delete lines except the lines with strings vj8436 Shell Programming and Scripting 14 04-17-2009 11:25 AM
print only matched string instead lines in grep anchal_khare Shell Programming and Scripting 3 02-18-2009 04:34 AM
How to print lines till till a pattern is matched in loop anoopvraj Shell Programming and Scripting 3 10-30-2008 10:36 PM
sed replaces all matched strings!!! melanie_pfefer Shell Programming and Scripting 1 08-07-2008 11:40 AM
SED: match pattern & delete matched lines not4google Shell Programming and Scripting 7 11-22-2006 09:58 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-27-2009
AMBER AMBER is offline
Registered User
  
 

Join Date: Mar 2009
Location: USA
Posts: 8
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 advance.
  #2 (permalink)  
Old 06-28-2009
tkleczek tkleczek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 34
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 (permalink)  
Old 06-28-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
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 (permalink)  
Old 06-28-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
awk '{print > $1".txt"}' file
Regards
  #5 (permalink)  
Old 06-29-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Code:
nawk '{file=sprintf("%s.txt",$1);print $0 >> file}' yourfile
  #6 (permalink)  
Old 06-29-2009
AMBER AMBER is offline
Registered User
  
 

Join Date: Mar 2009
Location: USA
Posts: 8
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
  #7 (permalink)  
Old 07-13-2009
pxgupta2k pxgupta2k is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 1
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0