Parsing alphanumeric variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Parsing alphanumeric variables
# 1  
Old 10-15-2011
Parsing alphanumeric variables

Hi All,

I have files with a column which has values and ranges, for example

colA colB
ERD1 3456
ERD2 [526887..526890]
ERD3 4456

I want to have the following output
colA colB colC
ERD1 3456 3456
ERD2 526887 526890
ERD3 4456 4456

Being a newbie to shell scripting ,I`m struggling to find the right commands.

Question 2.

I have an array of alphanumeric variables like 24S45M67N45M, 367M, 23M3D34M etc. There will be at least 1 M and at most 2 Ms in the strings
while the other alphabets may or may not be present.
I want to extract the number associated with the Ms. So the output will look like
24S45M67N45M 45 45
367M 367 0
23M3D34M 23 34

Please help !

Thanks,
Alpesh

So the ou at least 1
# 2  
Old 10-15-2011
Q1 :
Code:
sed '1s/.*/& colC/;s/[][]//g;s/\.\./ /' yourfile

Q2 :
Code:
$ for v in 24S45M67N45M 367M 23M3D34M
> do
> echo $v | nawk -F"M" '{gsub(/[0-9][0-9]*[^0-9M]/,z);print($1+0),($2+0)}'
> done
45 45
367 0
23 34

---------- Post updated at 11:04 PM ---------- Previous update was at 11:01 PM ----------

Also using the '+' from the regular expression extended character set :
Code:
$ for v in 24S45M67N45M 367M 23M3D34M
> do echo $v | nawk -F"M" '{gsub(/[0-9]+[^0-9M]/,z);print($1+0),($2+0)}'
> done
45 45
367 0
23 34

---------- Post updated at 11:10 PM ---------- Previous update was at 11:04 PM ----------

If you still want to display the initial value of the variable :
Code:
$ for v in 24S45M67N45M 367M 23M3D34M; do echo $v | nawk -FM '{x=$0;gsub(/[0-9]+[^0-9M]/,z);print x,($1+0),($2+0)}'; done
24S45M67N45M 45 45
367M 367 0
23M3D34M 23 34

---------- Post updated at 11:20 PM ---------- Previous update was at 11:10 PM ----------

Ooops i just realize that Q1 requires a fix :
Code:
nawk 'NR<2{print $0,colC;next}{gsub(/[][]/,z);sub(/\.\./,FS);$0=$0;print $1,$2,$NF}' yourfile

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-15-2011
Thanks ctsgnb . nawk doesnt work for my system but awk does Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a file and setting to variables.

greetings all, I have a task right now that is somewhat stumping me, and I am not sure what the best approach is to take it. I have a text file that will contain something similar to the following: first1, other1 first2, other2 first3, other3 first4, other4 I have to generate an... (14 Replies)
Discussion started by: jeffs42885
14 Replies

2. Shell Programming and Scripting

Parsing fields into variables

A record contains 50 fields separated by "~". I need to assign each of these fields to different variables. Following is the shell script approach I tried. RECORD="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5~...........~FIELD50" VAR1=$(echo ${RECORD} | cut -d"~" -f 1) VAR2=$(echo ${RECORD} | cut... (5 Replies)
Discussion started by: krishmaths
5 Replies

3. Shell Programming and Scripting

Comparing Alphanumeric Variables in Shell Script

Can someone please help me out here? I have strings similar to aafafaff45,29.34.942.45,edfdfafa that i want to compare to another similar string to check if they are the same. my script isn't working. ONE="aafafaff45,29.34.942.45,edfdfafa" TWO="ddfafagfa,87.57.942.45,afafafff" if ONE is... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. UNIX for Dummies Questions & Answers

Issue with parsing config variables

I am using MKS tool kit on windows server. One config variable is defined in windows environment and I am trying to use that variable. # Below RootDir is defined in windows RootDir="\\f01\var" # in unix script details="$RootDir/src|$RootDir/tgt" src=`echo $details|awk -F '|' '{print... (1 Reply)
Discussion started by: madhukalyan
1 Replies

5. Shell Programming and Scripting

Help parsing logs maybe with menu and variables?

I would like to parse through some logs looking for things like exception or failed (grep -i failed). Ideal would be if it were in a menu format so someone without unix ability could just choose option 1 2 or 3 etc. If I could pass the hostname to a variable also that would be awesome, so someone... (5 Replies)
Discussion started by: taekwondo
5 Replies

6. UNIX for Advanced & Expert Users

alphanumeric Sorting

Hi , I have a requirement where one column have to be sorted (delimiter is pipe) for eg: My input filed is as below 1|FIAT|0010103|23011|01/01/2000|31/12/9999|1.15 2|232|613|1 2|234|743|1 2|234|793|1 2|234|893|1 1|FIAT|0010103|23012|01/01/2000|31/12/9999|1.15 2|230|006|0 2|230|106|0... (9 Replies)
Discussion started by: laxmi131
9 Replies

7. Shell Programming and Scripting

Parsing Directory Names for Use as Bash Variables

Hi all: I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an... (6 Replies)
Discussion started by: aefskysa
6 Replies

8. Shell Programming and Scripting

Perl: parsing variables

I have the following script: #!/usr/bin/perl -w @files = <*.csv>; foreach $file (@files) { open(FH, $file); my @dt = split(/_|.csv/, $file); while (<FH>) { chomp; print $dt . $dt . ",$_\n"; } close(FH); } This script reads in all csv files in the current directory... (2 Replies)
Discussion started by: figaro
2 Replies

9. Shell Programming and Scripting

alphanumeric comparision

I have a requirement where I need to check if where r1v07l09ab is a software release. I should always check for this to be true to continue the release deployment because an older release should not be deployed by mistake. I mean only the release greater than the current release should be... (3 Replies)
Discussion started by: rakeshou
3 Replies

10. Shell Programming and Scripting

Parsing and getting values of variables

suppose i have a file value where it returns 3 values a=1 b=2 c=4 when i run it. i am using this file in my shell script. how do i parse and get the value of a b and c? (3 Replies)
Discussion started by: Rekha
3 Replies
Login or Register to Ask a Question