Split into columns based on the parameter and use & as delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split into columns based on the parameter and use & as delimiter
# 8  
Old 12-12-2010
Thanks Frans for your explanation...Its good day for me to learn such kind of scripting. Thanks a lot.
# 9  
Old 12-12-2010
Code:
awk -F\& 'BEGIN{m=split("poc&ooc&mjv&omj&mov&ps&rel&bod&oxi&ozn&lang&loc",T)}
         {s=x;for(i=1;i<=m;i++)for(j=1;j<=NF;j++)if($j~T[i])s=s (s==x?x:OFS FS) $j;print s}' OFS='\t' infile


Last edited by Scrutinizer; 12-12-2010 at 09:00 PM..
# 10  
Old 12-12-2010
Code:
awk -F\& '{r=x;for(i=split("poc&ooc&mjv&omj&mov&ps&rel&bod&oxi&ozn&lang&loc",F);i;i--)
for(j=0;j<NF;)if($++j~F[i])r=$j (r?"\t"FS r:x);print r}' infile


Last edited by Chubler_XL; 12-12-2010 at 10:51 PM..
# 11  
Old 12-13-2010
The use of eval to execute input data is dangerous stuff because it allows a malicious user with access to the data file to execute any code as the user who executes the script.

Alternative shell script:
Code:
# oldIFS=$IFS
IFS='&'
while read line
do
  for f in poc ooc mjv omj mov ps rel bod oxi ozn lang loc
  do
    for i in $line
    do
      case $i in
        $f=*) printf "%s\t" "$i"
      esac
    done
  done
  echo
done <infile
# IFS=$oldIFS

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 12  
Old 12-16-2010
Question

Quote:
Originally Posted by Scrutinizer
The use of eval to execute input data is dangerous stuff because it allows a malicious user with access to the data file to execute any code as the user who executes the script.

Alternative shell script:
Code:
# oldIFS=$IFS
IFS='&'
while read line
do
  for f in poc ooc mjv omj mov ps rel bod oxi ozn lang loc
  do
    for i in $line
    do
      case $i in
        $f=*) printf "%s\t" "$i"
      esac
    done
  done
  echo
done <infile
# IFS=$oldIFS

When going with the above script if any of the column/search string is not available (on f) I need to print NULL for the variable.

say for example if i don't find 'rel' & 'bod' in the line while reading the line, I want print rel=NULL and bod=NULL

I am trying out with lot of combination ..not able to achieve it.

Last edited by elamurugu; 12-16-2010 at 02:30 PM..
# 13  
Old 12-16-2010
Could this help ?
bash code:
  1. #!/bin/bash
  2. while read L
  3. do
  4.    for V in poc ooc mjv omj mov ps rel bod oxi ozn lang loc drc
  5.    do
  6.       Val=$(echo "$L" | grep -oE "$V=[^&]*" | sed 's/.*=//')
  7.       echo -en "$V=${Val:-NULL}\t"
  8.    done
  9.    echo
  10. done <$INFILE
# 14  
Old 12-17-2010
Code:
# oldIFS=$IFS
IFS='&'
while read line
do
  for f in poc ooc mjv omj mov ps rel bod oxi ozn lang loc
  do
    val=
    for i in $line
    do
      case $i in
        $f=*) val=$i; break
      esac
    done
    printf "%s\t" "${val:-$f=NULL}"
  done
  echo
done <infile
# IFS=$oldIFS

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split files based on row delimiter count

I have a huge file (around 4-5 GB containing 20 million rows) which has text like: <EOFD>11<EOFD>22<EORD>2<EOFD>2222<EOFD>3333<EORD>3<EOFD>44<EOFD>55<EORD>66<EOFD>888<EOFD>9999<EORD> Actually above is an extracted file from a Sql Server with each field delimited by <EOFD> and each row ends... (8 Replies)
Discussion started by: amvip
8 Replies

2. UNIX for Dummies Questions & Answers

Split 1 column into numerous columns based on patterns

Hi, I have a text file 'Item_List.txt' containing only 1 column. This column lists different products, each separated by the same generic string header "NEW PRODUCT, VERSION 1.1". After this the name of the product is given, then a delimiter string "PRODUCT FIELD", and then the name of the... (11 Replies)
Discussion started by: mmab
11 Replies

3. Shell Programming and Scripting

awk script to split file into multiple files based on many columns

So I have a space delimited file that I'd like to split into multiple files based on multiple column values. This is what my data looks like 1bc9A02 1 10 1000 FTDLNLVQALRQFLWSFRLPGEAQKIDRMMEAFAQRYCQCNNGVFQSTDTCYVLSFAIIMLNTSLHNPNVKDKPTVERFIAMNRGINDGGDLPEELLRNLYESIKNEPFKIPELEHHHHHH 1ku1A02 1 10... (9 Replies)
Discussion started by: viored
9 Replies

4. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

5. Shell Programming and Scripting

getting the last parameter through awk & split

Hi, I would like to know how to extract and get the value of the last element in an array after split. Let's say i have an input of aaa mykeyword_111 abc/def/ghi/mno bbb none ttt.aaa.ccc ccc mykeyword_222 ppp/qqq/rrr/ss I expect the output to be like this way. aaa 111 mno ccc... (4 Replies)
Discussion started by: rei125
4 Replies

6. Shell Programming and Scripting

split record based on delimiter

Hi, My inputfile contains field separaer is ^. 12^inms^ 13^fakdks^ssk^s3 23^avsd^ 13^fakdks^ssk^a4 I wanted to print only 2 delimiter occurence i.e 12^inms^ 23^avsd^ (4 Replies)
Discussion started by: Jairaj
4 Replies

7. Shell Programming and Scripting

split the file based on the 2nd column passing as a parameter

I am unable to spit the file based on the 2nd column passing as a parameter with awk command. Source file: “100”,”customer information”,”10000” “200”,”customer information”,”50000” “300”,”product information”,”40000” script: the command is not allowing to pass the parameters with the awk... (7 Replies)
Discussion started by: number10
7 Replies

8. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

9. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

10. Shell Programming and Scripting

Remove lines, Sorted with Time based columns using AWK & SORT

Hi having a file as follows MediaErr.log 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:12:16 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:22:47 84 Server1 Policy1 Schedule1 master1 05/08/2008 03:41:26 84 Server1 Policy1 ... (1 Reply)
Discussion started by: karthikn7974
1 Replies
Login or Register to Ask a Question