Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-25-2011
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.
# 2  
Old 08-25-2011
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)
# 3  
Old 08-25-2011
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.
# 4  
Old 08-25-2011
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)
# 5  
Old 08-25-2011
Even better

---------- 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.
# 6  
Old 08-25-2011
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-25-2011)
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash - Find files excluding file patterns and subfolder patterns jcdole Shell Programming and Scripting 2 09-21-2017 11:25 AM
Extract lines between patterns squrcles Shell Programming and Scripting 10 01-18-2016 12:15 PM
Extract all the sentences that matched two patterns my_Perl Shell Programming and Scripting 1 06-23-2015 08:45 PM
Extract Lines Between 2 patterns if exist... DSommers Debian 3 09-29-2014 05:44 PM
Need to extract text repetitively between two patterns Vignesh58 Shell Programming and Scripting 2 02-21-2013 06:53 AM
Extract line between two patterns rajamohan Shell Programming and Scripting 4 12-03-2012 01:55 AM
Search and extract matching patterns lucasvs UNIX for Dummies Questions & Answers 9 02-28-2012 04:04 AM
PERL: extract lines between two patterns mnithink Shell Programming and Scripting 11 01-23-2012 10:58 PM
Extract certain patterns from file. shoaibjameel123 Shell Programming and Scripting 2 07-08-2011 09:56 AM
To extract the string between two patterns aajan Shell Programming and Scripting 6 09-17-2007 02:41 AM


All times are GMT -4. The time now is 05:57 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password