Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Extract patterns and copy them in different files

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-25-2011   -   Original Discussion by shoaibjameel123
shoaibjameel123 shoaibjameel123 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 11 October 2017, 1:35 PM EDT
Location: Nowhere
Posts: 190
Thanks: 227
Thanked 1 Time in 1 Post
Extract patterns and copy them in different files

Hi All,

I have a file which looks like this:


Code:
Name1;A01
Name2;A01.047
Name3;A01.047.025
Newname1;B01
NewName2;B01.056.32
NewName3;B04.09.43
NewNewName1;C01.03
NewNewName2;C01.034.44

As you can see, in the file there is some name and followed by the name is some identifier. These identifiers have same identification alphabet (A, B, C until Z) if they belong to the same type.

My task is to extract all the names which belong to the same alphabet and store them in separate files.

For example,
A.dat will look like this:

Code:
Name1;A01
Name2;A01.047
Name3;A01.047.025

B.dat will look like this:

Code:
Newname1;B01
NewName2;B01.056.32
NewName3;B04.09.43

and C.dat
Code:
NewNewName1;C01.03
NewNewName2;C01.034.44

I am using Linux with BASH shell.
Sponsored Links
    #2  
Old Unix and Linux 08-25-2011   -   Original Discussion by shoaibjameel123
yazu yazu is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 2 August 2017, 9:27 AM EDT
Location: From far
Posts: 1,000
Thanks: 21
Thanked 237 Times in 231 Posts

Code:
awk -F';' '{ print $0 >> substr($2, 1, 1) }' INPUTFILE

The Following User Says Thank You to yazu For This Useful Post:
shoaibjameel123 (08-25-2011)
Sponsored Links
    #3  
Old Unix and Linux 08-25-2011   -   Original Discussion by shoaibjameel123
shoaibjameel123 shoaibjameel123 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 11 October 2017, 1:35 PM EDT
Location: Nowhere
Posts: 190
Thanks: 227
Thanked 1 Time in 1 Post
And this is how I would add the extensions to files:


Code:
for i in `ls PATH`
do
        mv $i $i.dat
done

Thanks for your help and this completes everything. Linux
    #4  
Old Unix and Linux 08-25-2011   -   Original Discussion by shoaibjameel123
yazu yazu is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 2 August 2017, 9:27 AM EDT
Location: From far
Posts: 1,000
Thanks: 21
Thanked 237 Times in 231 Posts
Well, sorry, I didn't notice:

Code:
awk -F';' '{ print $0 >> substr($2, 1, 1) ".dat" }' INPUTFILE

The Following User Says Thank You to yazu For This Useful Post:
shoaibjameel123 (08-25-2011)
Sponsored Links
    #5  
Old Unix and Linux 08-26-2011   -   Original Discussion by shoaibjameel123
shoaibjameel123 shoaibjameel123 is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 11 October 2017, 1:35 PM EDT
Location: Nowhere
Posts: 190
Thanks: 227
Thanked 1 Time in 1 Post
Even better Linux

---------- Post updated at 11:11 AM ---------- Previous update was at 10:17 AM ----------

I am now trying to find those lines which have ZERO dot and store those lines in A.dat and then the lines which have 1 dot store them in B.dat and two dots store them in C.dat and so on until I reach the final number of dots at maximum they are 11 and less.

This is an illustration:

main_file.txt which looks like this:


Code:
Name1;A01
Name2;A01.047
Name3;A01.047.025
Newname1;B01
NewName2;B01.056.32
NewName3;B04.09.43
NewNewName1;C01.03
NewNewName2;C01.034.44

So, this is what I expect:
A.dat


Code:
Name1;A01
Newname1;B01

B.dat


Code:
Name2;A01.047

C.dat


Code:
Name3;A01.047.025
NewName2;B01.056.32
NewName3;B04.09.43
NewNewName2;C01.034.44

This is what I tried but to no success:


Code:
perl -lne '$c=1 while /./g; END { print $c."dat"; }'

Even the part that gives file names is not perfect.
Sponsored Links
    #6  
Old Unix and Linux 08-26-2011   -   Original Discussion by shoaibjameel123
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 5 November 2017, 9:59 PM EST
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts

Code:
nawk 'BEGIN{FS="";count=0}  {for(i=1;i<=length($0);i++) {if(substr($0,i,1)==".") count++;} {print $0 >> count;count=0}}' main_file.txt

This will create a files 0,1,2...etc depends upon the .(dot) which is in the line
The Following User Says Thank You to itkamaraj For This Useful Post:
shoaibjameel123 (08-26-2011)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract certain patterns from file. shoaibjameel123 Shell Programming and Scripting 2 07-08-2011 10:56 AM
How to group matched patterns in different files redse171 Shell Programming and Scripting 2 03-14-2011 08:51 PM
AWK: matching patterns in 2 different files asanjuan Shell Programming and Scripting 5 09-14-2010 07:15 AM
Searching for multiple patterns in files bornon2303 Shell Programming and Scripting 5 02-25-2010 10:05 AM
To extract the string between two patterns aajan Shell Programming and Scripting 6 09-17-2007 03:41 AM



All times are GMT -4. The time now is 12:58 AM.