Parse based on location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse based on location
# 1  
Old 04-17-2012
Parse based on location

I do have a big text file
Code:
>A1
ACFGTSDCHG_GDF
DGHA
>A2
A_FGGSD_HG_GSF
DGTA
>A3
AC_GSDD_TU_GDF
DGH_
..
...

there are equal number of characters within all the sub-headers. I want to parse out all the characters under each sub-header from user specified locations. For example, I want to parse out the characters at 5, 8, 11, 18 positions so that the output file looks like this:
Code:
>A1
TC_A
>A2
G__A
>A3
S___

Please let me know the best way to implement it using awk
# 2  
Old 04-17-2012
Hi, try this:
Code:
sed '/^>/{n;N;s/\n//;s/....\(.\)..\(.\)..\(.\)....../\1\2\3/;}' infile

Code:
awk 'NR>1{printf RS}; sub(/\n.*/,"\n" substr($2,5,1) substr($2,8,1) substr($2,11,1) substr($3,4,1) "\n")' RS=\> ORS= infile

Some awks:
Code:
awk 'NR>1{printf RS $1 $2 $3 $8 $11 $14 $22 "\n"}' FS= RS=\> ORS= infile


Last edited by Scrutinizer; 04-17-2012 at 05:52 PM..
# 3  
Old 04-17-2012
It worked. But this is for a specified locations.. How would I specify my own locations. Also my sub-headers are actually 1000's not 3.
# 4  
Old 04-17-2012
How about this, just change the F= string to the csv of locations you want:

Code:
awk -vF=5,8,11,18 '
BEGIN{w=split(F,T,",")}
NR>1{
   print RS substr($0,1,3)
   gsub(/\n/,"",$0)
   for(i=1;i<=w;i++) printf "%s", substr($0,T[i]+2,1)
   printf "\n" }' RS=\> ORS= infile

# 5  
Old 04-17-2012
Thanks it worked for the example data, but for my data where under each subheader has 17 lines. didn't work. I guess I may have to modify the substr step..right?
# 6  
Old 04-17-2012
I suspect that your header isn't always 3 chars as it is in the example
Try this:

Code:
awk -vF=5,8,11,18 '
BEGIN{w=split(F,T,",")}
NR>1{
   print RS $1
   a=length($1)
   gsub(/\n/,"",$0)
   for(i=1;i<=w;i++) printf "%s", substr($0,T[i]+a,1)
   print "" }' RS=\> infile


Last edited by Chubler_XL; 04-17-2012 at 08:22 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Apache configuration Automation based on location

Hi I need to find a way to automate the deployment of my apache configuration to a few servers. I make change frequently on apache/conf/... etc, and I usually do this manually by copying files to each servers(and country config path)and restart apache servers.Source file will be like... (1 Reply)
Discussion started by: naresh2389
1 Replies

2. Shell Programming and Scripting

Parse line based on highest value

Hello, Does anyone know how to parse whole line that has highest value in last column (c12), based on the string in column 6 (column VIRUS) INPUT: 5 7 4 8 3 VIRUS1 5 5 HUMAN1 8 4 100 6 9 7 2 6 VIRUS1 6 0 HUMAN2 6 2 80 9 6 7 3 7 VIRUS1 9 7 HUMAN3 5 9 70 8 5 3 8 6 VIRUS2 8 6 HUMAN4 8 6 95... (3 Replies)
Discussion started by: narachaid
3 Replies

3. Shell Programming and Scripting

How to copy files from one location to another based on a priority?

Hi Gurus, I am a newbie to shell scripting and I am facing a problem right now.I have to automate the copy of files based on a priority.The scenario is as below: 1) There will be files from Mon-Fri with Mon file being named as abc_def_01_YYYYMMDD and Tue file being abc_def_02_YYYYMMDD and so... (4 Replies)
Discussion started by: vikramgk9
4 Replies

4. Web Development

PHP client location based page loading

Hi, I am new to PHP and at first got a task where I need some clues and help. So I am here.. The server is build to provide customized access for modules as per location. If client location is US then it shows modules and pages specific to US and if location is UK then it shows UK specific... (1 Reply)
Discussion started by: zing_foru
1 Replies

5. Web Development

HTML/PHP show IMG based on location

HI guys/gals, I like to add some personal touches to a welcome image on our mediawiki site, and have the welcome be displayed in the, presumed, language of the visitor. Standard would be English, but if the IP or browser or OS is set to be in India, I want to display the message in Urdu for... (5 Replies)
Discussion started by: lawstudent
5 Replies

6. Shell Programming and Scripting

AWK Command parse a file based on string.

AWK Command parse a file based on string. I am trying to write a shell script to parse a file based on a string and move the content of the file to another file. Here is scenario. File content below Mime-Version: 1.0 Content-Type: multipart/mixed; ... (2 Replies)
Discussion started by: aakishore
2 Replies

7. Shell Programming and Scripting

Help with replace character based on specific location

Hi, I got long list of reference file (column one is refer to the header in input file; column 2 is info of start position in input file; column 3 is info of end position in input file;) shown as below: read_2 10 15 read_3 5 8 read_1 4 10 . . . Input file (huge file with total... (6 Replies)
Discussion started by: perl_beginner
6 Replies

8. OS X (Apple)

Location Based Printing in Leopard not working

Hello all, I've been searching for an answer but coming up with nothing so I figured I'd give it a shot and ask. After installing a fresh version of Leopard (with the latest updates) on a computer and then copying the user data back onto said computer, the location based printing doesn't work... (0 Replies)
Discussion started by: erodrigue
0 Replies

9. Shell Programming and Scripting

how to detect ip address based location using PHP

I am interested in finding out if there is a reliable and economical way for extracting user's location based on his ip address, using php I tried to get the job done using ip2location.com, but I am not happy how it deals with result inconsistently. (2 Replies)
Discussion started by: siftin-com
2 Replies

10. UNIX for Dummies Questions & Answers

copying files from one location to another based on a list

I have a text list of about 3,000 file names (image files), which exist on a server and that I want to copy over to another location. I understand the Unix cp code, but what's the string to have it copy multiple files based on an external list? Many thanks! (4 Replies)
Discussion started by: rebornhonest
4 Replies
Login or Register to Ask a Question