Need help with shell, trying to append or separate values in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with shell, trying to append or separate values in a string
# 1  
Old 04-10-2011
Need help with shell, trying to append or separate values in a string

Ok. I for the life of me cant figure out how to do this. I need Help. So here is what I'm trying to do. I have a block of text. They are FIPS codes for counties. Below is the block. There are probably a few ways to do this. The first line starting with ARC021....... this line is a list of Arkansas counties, until you get to the part that says MOC069, those two (069 and 155) are Missouri counties. You get the idea. MSC### is Mississippi, and TNC### is Tennessee. Each state has a prefix number that applies to the code. Its not shown here, however I know them. For example, for Arkansas its 05 for Tennessee its 47. So I need to read this block of text and output the appropriate prefix number in front of the county number and output them all to a file. For instance I'll do the first 3 in line1. Also, the number of lines can change. They will always be formatted this way, just that there may only be two lines or just one.

Desired Output:
05021
05031
05035........

So essentially the code would know the state by reading the "ARC", "MOC", "MSC", and "TNC", then going from left to right, apply each state prefix to each county code, then output to a file. The very last 6 digit number in the block of text is not needed. Confused now?

Sample Block of Text:
ARC021-031-035-037-055-075-077-093-107-111-121-123-MOC069-155-
MSC003-009-027-033-071-093-107-119-135-137-139-143-145-161-
TNC017-023-033-039-045-047-053-069-071-075-077-079-095-097-109-113-131-
157-167-183-042000-

Last edited by chagan02; 04-10-2011 at 08:28 PM..
# 2  
Old 04-10-2011
Code:
$ cat country.list (for example)

ARC 05
TNC 47
MOC 22
MSC 88

$ cat FIPS.list

ARC021-031-035-037-055-075-077-093-107-111-121-123-MOC069-155-
MSC003-009-027-033-071-093-107-119-135-137-139-143-145-161-
TNC017-023-033-039-045-047-053-069-071-075-077-079-095-097-109-113-131-
157-167-183-042000-

run the awk command, you will get the result.

Code:
awk 'NR==FNR{a[$1]=$2;next} 
     {if (length($1)>3) {country=substr($1,1,3);code=substr($1,4);print a[country] code} 
         else {print a[country] $1}
     }' country.list RS="-" FIPS.list

# 3  
Old 04-10-2011
Thanks Man! Works like a charm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. UNIX for Dummies Questions & Answers

How to append values to a string?

Hi, Requesting some help with a problem I am facing with string function in UNIX. I wish to create 2 string variables: 1st header string containing output_1, output_2, .. , output_<n> and 2nd data string containing the filename separated by colon (":") and corresponding filesize separated by... (6 Replies)
Discussion started by: vkumbhakarna
6 Replies

3. Shell Programming and Scripting

Divide data with specific column values into separate files

hello! i need a little help from you :) ... i need to split a file into separate files depending on two conditions using scripting. The file has no delimiters. The conditions are col 17 = "P" and col 81 = "*", this will go to one output file; col 17 = "R" and col 81 = " ". Here is an example. ... (3 Replies)
Discussion started by: chanclitas
3 Replies

4. AIX

How to append spaces to string values?

i/o file: abc,efg,xyz Required o/p file: "abc (Value + blank spaces=16) " ,"efg (Value +blank spaces=15) " ,"xyz (Value+ blank spaces =20) " In short input file value stores in result file with " i/p Value " added with spaces and are of fixed size like 16,15,20 How to do using... (2 Replies)
Discussion started by: AhmedLakadkutta
2 Replies

5. Shell Programming and Scripting

Shell scripting -append a new string

hi all , i am looking for a shell script which looks into a directory of text files and searches for a string(abc123) in all the text files and if that exists add a new line(abc124) in all the *.txt files automatically, or if (abc124) exists add (abc123) can you all please help me. (6 Replies)
Discussion started by: joseph.dmello
6 Replies

6. Shell Programming and Scripting

How to separate the values in perl??

Hi, I have a string like that and i have to separate the key and value pair. $str="OWN - NLM STAT- MEDLINE DA - 19990311 DCOM- 19990311 LR - 20061115 AB - The perlfaq comprises several documents that answer the most commonly asked questions about Perl and Perl programming. It's divided... (3 Replies)
Discussion started by: vanitham
3 Replies

7. Shell Programming and Scripting

Separate String Shell Script

Hi guys, I am a beginner in shell script.. How can I separate a string coming from a parameter in spaces ? Ex: input: test.sh abcd output: a b c d Thanks Dusse (15 Replies)
Discussion started by: dusse
15 Replies

8. Shell Programming and Scripting

Append values before a string

hi all, i have variables a and b with values, like a="/var/tmp/new.sh /var/tmp/new2.sh" b="/TEST" how i need to append the value "/TEST" before the values for the variable "a" so that i get the output as /TEST/var/tmp/new.sh /TEST/var/tmp/new2.sh plz help me Regards, NG (2 Replies)
Discussion started by: Nandagopal
2 Replies

9. UNIX for Dummies Questions & Answers

to separate values from a string

Hi I would like to take input from user like username/password@connectstring I should be able to cut the username and password and connect string for example if someone enters like sam/sammy@ora1 my program should take sam as username sammy as password and ora1 as connectstring and... (3 Replies)
Discussion started by: ssuresh1999
3 Replies

10. UNIX for Advanced & Expert Users

insert pipe in file to separate values

hi all... i need your help, because i donīt know what to do... i have a flat file like this: B065200512312004123111010000061451 000021853 B065200512312004123111020000621907 000417802 B065200512312004123111030000005214 000005861 B065200512312004123111040000120133 000088448 and i need... (5 Replies)
Discussion started by: DebianJ
5 Replies
Login or Register to Ask a Question