Help required with awk/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help required with awk/sed
# 1  
Old 07-06-2009
Help required with awk/sed

Hi

I have a file, with format like:

column1|coulumn2|column3|column4
A|X|K|18
L|O|R|31,42,25
G|H|I|55,66
L|E|Q|25,31,94

output required:

column1|coulumn2|column3|column4
A|X|K|18
L|O|R|31,25
L|E|Q|25,31

Input File Format: All columns are seperated using |, last column might have multiple values spearated with ,

Output file rules:

1. Only keep rows which has one of the 18,25 or 31 (in its comma separated values) in the last column's comma separated values.
2. if last column contains any of the 18,25,31 values and some any other number, then remove numbers other than 18,25,31 as in the example output file - 1st row remains as it is, in 2nd 42 removed as this does not belong to the set (18,25,31), 3rd line remove altoghther as this does not have any of the (18,25,31) in its comma seperaetd values, 4th row remains but 94 goes away.

Please help writing one liner using awk or sed.

Thanks.
# 2  
Old 07-06-2009
Why don't you show us the code you have so far, and tell us how it misbehaves?

Regards
# 3  
Old 07-06-2009
I managed to write when I had to make values null in the last column:

awk -v numLines=`cat data.out.20090526 | wc -l` 'BEGIN { FS=OFS="|" } {if(NR > 1 && NR < numLines) {$4="null"} } 1' data.out.20090526 > data_new

but I am not sure how to check the values in last column against the given set of values (problem is with spliting the values in last column and then checking those and then removing row or values). Not sure if all this is possible in one awk/sed statement.

Please help, if you know how this can be done!!!!
# 4  
Old 07-06-2009
Try this:

Code:
awk 'BEGIN{FS=OFS="|"; b[18]=b[25]=b[31]}{
  n=split($4,a,",")
  for(i=1;i<=n;i++){
    if(a[i]in b) {
      s=s?s "," a[i]:a[i]
    }
  }
}
s{print $1,$2,$3,s;s=""}
' file > newfile

# 5  
Old 07-06-2009
Thanks a lot, this works (When I create a shell script with this content).

Is there any better way to print everything here rather than giving columns like $1,$2 etc, because I have a file having more than 100 coulmns in it?

One more question (That's a dumb one though): When I copy this and move everything in one line, I get syntax error, I tried to play around with the spaces before curly braces and all but could not make this work.

But thanks a lot for the help.

Thanks.
# 6  
Old 07-06-2009
You should use nawk or gawk if you have more than 100 columns.
If you have more columns you can use this code, assuming the multiple values separated by commas is in the last column:

Code:
awk 'BEGIN{FS=OFS="|"; b[18]=b[25]=b[31]}{
  n=split($NF,a,",")
  for(i=1;i<=n;i++){
    if(a[i]in b) {
      s=s?s "," a[i]:a[i]
    }
  }
}
s{$NF=s;s="";print}
' file > newfile

# 7  
Old 07-07-2009
awk:
Code:
nawk  'BEGIN{FS=OFS="|"}
/(18|31|25)/{
	tmp=""
	n=split($4,arr,",")
	for(i=1;i<=n;i++)
		if(arr[i]==18 || arr[i]==31 || arr[i]==25)
			if(tmp=="")
				tmp=arr[i]
			else
				tmp=sprintf("%s,%s",tmp,arr[i])
	$4=tmp
	print
}' yourfile

perl:

Code:
while(<DATA>){
	chomp;
	if(/[^|]*(18|25|31)[^|]*$/){
		my @tmp=split("[|]",$_);
		my @t=split(",",$tmp[3]);
		$tmp[3] = join ",", grep {$_==18 or $_==25 or $_==31} @t;
		print join "|", @tmp;
		print "\n";
	}
}
__DATA__
A|X|K|18
L|O|R|31,42,25
G|H|I|55,66
L|E|Q|25,31,94


Last edited by summer_cherry; 07-07-2009 at 01:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

REGEX help required and some sed/awk help as well

Hi guys, I am coding a bash script that makes use of php scripts to pull URL's from a website. These url links will have numbers in them like 0.2.3 I want to make a regex that will yield me such numbers if I use a command like preg_grep. Question1: I need a regex that will tell my preg_grep... (2 Replies)
Discussion started by: mojoman
2 Replies

2. Shell Programming and Scripting

Sed/awk : to grep only required pattern disk

Hi Experts, Need help with the following: Desired output: Only want to get the output marked in green. The file: --- Physical volumes --- PV Name /dev/disk/disk4704 PV Status available Total PE 6399 Free PE ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Tricky sed required

Hi All I need to put some sed together for a task and its a bit advanced for me, so I thought I'd ask if anyone here could help. I have a csv file with content like this - "","abcde","","" "'","abcde","","" "","","","1234" "'e'","","","" I need to remove any single quotes that fall... (17 Replies)
Discussion started by: steadyonabix
17 Replies

4. Shell Programming and Scripting

Want to sort a file using awk & sed to get required output

Hi All, Need Suggestion, Want to sort a file using awk & sed to get required, output as below, such that each LUN shows correct WWPN and FA port Numbers correctly: Required output: 01FB 10000000c97843a2 8C 0 01FB 10000000c96fb279 9C 0 22AF 10000000c97843a2 8C 0 22AF 10000000c975adbd ... (10 Replies)
Discussion started by: aix_admin_007
10 Replies

5. UNIX for Dummies Questions & Answers

Clarification required on sed

Hi Can some one tell what does this sed command do sed 's/*$//g I am more curious on the highlighted part , can some one explain what does that mean. Thanks Sri (1 Reply)
Discussion started by: Sri3001
1 Replies

6. Shell Programming and Scripting

sed help required

Hi All, I have one file with below type of data in it, $ cat test.txt ###123 ###xyxytuerwb ###2 ###tyupe Here I would like to replace all the characters with "x" after the 3 "###" with the same number of characters. Can you please help me to achieve this. (7 Replies)
Discussion started by: gr8_usk
7 Replies

7. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

8. Shell Programming and Scripting

Help required in sed or awk.

Hi All, I need to pick up data on both sides of "=" sign. For eg, following is the context that I have. 125.156.125.147=machine1 147.125.185.156=machine2 147.125.185.159=machine3 Can I have the ip address in one variable and machine name in another variable using sed or awk. ... (1 Reply)
Discussion started by: nua7
1 Replies

9. Shell Programming and Scripting

Help in sed required.

Hi All, I am facing a small problem in sed. I want to insert a line in the existing file. Existing code: access to attr=userPassword by self write by * auth access to * by self write by users read by anonymous auth Desired code: access to attr=userPassword by self... (14 Replies)
Discussion started by: nua7
14 Replies

10. Shell Programming and Scripting

sed script required

Collegues I have a flat file with data in following structure. (S1 (S (NP (NP (DT The) (JJ beautiful) (NN view)) (PP (IN of) (NP (JJ outside) (NNP greenery)))) (VP (VBZ adds) (NP (DT a) (NN tranquil) (NN touch)) (PP (TO to) (NP (DT this) (NN place)))) (. .))) I have to extract the contents in... (2 Replies)
Discussion started by: jaganadh
2 Replies
Login or Register to Ask a Question