awk or sed help urgent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed help urgent
# 1  
Old 03-04-2010
awk or sed help urgent

Hello,

I have a file with the follwoing pattern:

Input file:
===========
Code:
tcp://xxx:123
8179          YY
1798          YY
 
tcp://abc:2345
not found
 
tcp://swt:4945
7356        QQ
 
tcp://pqr:456
8178       PP
9485       PP
4485       PP

I am after a pipe seperated file with the first filed like below. Can somebody help me getting a oneliner....this is quite urgent...


I tried the below code but didnt get the result I am after..

==============
Code:
for i in `cat input.txt`
do
   
   str=`echo $i | cut -d':' -f1 | sed 's/\"//'`
   
   if [ $str -ne "tcp" ]; then
    mainStr=$mainStr","$i
   else
    echo $mainStr >> output.txt
        mainStr=$i
 mainStr=$mainStr"|"
   fi

======================

Output file:
============
Code:
tcp://xxx:123|8179,1798
tcp://abc:2345| not
tcp://swt:4945|7356
tcp://pqr:456|8178,9485,4485


Last edited by pludi; 03-04-2010 at 05:14 AM.. Reason: code tags, please...
# 2  
Old 03-04-2010
Code:
awk '/^tcp/{printf("%s|",$0) }!/^tcp/ && !/^$/{printf("%s,",$1)}/^$/{print}' filename | sed 's/\,$//g'


cheers,
Devaraj Takhellambam
# 3  
Old 03-04-2010
or simpler...

Code:
awk 'NF>0{printf ($1 ~ /^tcp*/?"\n"$1:"|"$1)}' infile

# 4  
Old 03-04-2010
You can achieve this by using following script.

Code:
while read line
do
echo $line
str=`echo $line | cut -d':' -f1 `
if [[ $str == "tcp" ]]; then
echo -en "\n" >> output.txt
echo -n "$line|" >> output.txt
else
str=`echo $line | cut -d ' ' -f 1`
echo -n "$str,">> output.txt
fi
done < input.txt

sed -ri 's/[,]+$//g;/^$/d' output.txt

# 5  
Old 03-04-2010
use below:-

Code:
nawk '/^tcp/{printf "\n"$0"|" ; next}NF{printf $1"," ; next}'  infile.txt

SmilieSmilieSmilie

---------- Post updated at 11:46 ---------- Previous update was at 11:42 ----------

Quote:
Originally Posted by malcomex999
or simpler...

Code:
awk 'NF>0{printf ($1 ~ /^tcp*/?"\n"$1:"|"$1)}' infile


the above code will not produce the desired o/p :-

Code:
tcp://xxx:123|8179,1798
tcp://abc:2345| not
tcp://swt:4945|7356
tcp://pqr:456|8178,9485,4485

it will produce :-

Code:
tcp://xxx:123|8179|1798
tcp://abc:2345|not
tcp://swt:4945|7356
tcp://pqr:456|8178|9485|4485


Last edited by ahmad.diab; 03-04-2010 at 05:56 AM..
# 6  
Old 03-04-2010
Quote:
Originally Posted by ahmad.diab
use below:-

Code:
nawk '/^tcp/{printf $0"|" ; next}NF{printf $1"," ; next}{print ""}'  infile.txt

SmilieSmilieSmilie

---------- Post updated at 11:46 ---------- Previous update was at 11:42 ----------




the above code will not produce the desired o/p :-

Code:
tcp://xxx:123|8179,1798
tcp://abc:2345| not
tcp://swt:4945|7356
tcp://pqr:456|8178,9485,4485

it will produce :-

Code:
tcp://xxx:123|8179|1798
tcp://abc:2345|not
tcp://swt:4945|7356
tcp://pqr:456|8178|9485|4485

Yea, thanks for pointing out that but yours will give extra comma at the end.

But this can do what the requester wants...

Code:
awk 'NF{printf ($1 ~ /^tcp*/?"\n"$1"|":$1",")}' infile | sed 's/\,$//'


Last edited by malcomex999; 03-04-2010 at 05:57 AM..
# 7  
Old 03-04-2010
Quote:
Originally Posted by malcomex999
Yea, thanks for pointing out that.
just a small modification to your code and it will give the desired o/p:-


Code:
nawk 'NF{printf ($1 ~ /^tcp*/?"\n"$1"|":$1",")}' infile.txt

SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Urgent help on awk/sed

Hi, Sample Input Table 1 XXXXX YYYYY A 1 2 3 4 5 B 1 2 3 4 5 C 1 2 3 4 5 D 1 2 3 4 5 A 6 7 B 6 7 C 6 7 D 6 7 Table 2 XXXXX YYYYY E 1 2 3 4 5 F 1 2 3 4 5 E 6 7 F 6 7 Table 3 XXXXX YYYYY G 1 2 3 4 5 (4 Replies)
Discussion started by: ravin
4 Replies

2. Shell Programming and Scripting

Urgent solution for simple sed

Hi Im running this command on AIX in ksh. My input file samp1 contains 1 2 123 12345 When I execute the following sed i dont get a matching pattern sed -n '/{1}/p' samp1 Can anyone help me with this simple thing (3 Replies)
Discussion started by: raghav288
3 Replies

3. Infrastructure Monitoring

sed help,,kind of urgent!!

Hello All, My problem is: I want to replace a line from a file with sed. The first word in that line is always the same in every server. The second line is server model, which of course will vary from platform to platform and I need to leave that word as it is. After the second word, I need to... (3 Replies)
Discussion started by: solaix14
3 Replies

4. 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

5. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern 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... (2 Replies)
Discussion started by: rajan_san
2 Replies

6. Shell Programming and Scripting

Need Help using sed command(very urgent)

Hi all, Actually i want to delete the .ps extension from package1.ps string by using sed. Can any body tell me that how shell i do it????????? It is very urgent. Can anybody help me. I am trying to do this in the following way. ps_file="package1.ps" echo $ps_file sed s/.ps//g $ps_file... (9 Replies)
Discussion started by: sunitachoudhury
9 Replies

7. Shell Programming and Scripting

Urgent Help With Sed

Hello, I need to use sed to replace a word in file. My command is this: sed "s/word_to_replace/'"${INPUT}"'/1 filename and because INPUT="~@#$%^&*()-_=+{}\|;:<>,./?" and / is also the delimiter so I'm keep on getting error message sed: command garbled: ... any suggestions about how I... (10 Replies)
Discussion started by: katrvu
10 Replies

8. Shell Programming and Scripting

sed script help urgent

Collegues I am dealing with raw text files which is extracted from web pages. I have to find sentances which contains more than 99 words and have to put a "." after the 99th or 98 th word . Is there any possiblity to it in sed or awk With regards Jaganadh.H (5 Replies)
Discussion started by: jaganadh
5 Replies

9. Shell Programming and Scripting

Need urgent sed help

I am writing a shell script on SunOS cosuaah01 5.9 Generic_118558-11 sun4u sparc SUNW,Sun-Fire-V440 machine. The shell script in.sh looks like this: install_top=`pwd` fl2=/d01/applptch/ptchora/iAS/Apache/Apache/cgi-bin/cxtool/display_report.pl echo $fl2 mv $fl2 $fl2.old sed 5c\... (6 Replies)
Discussion started by: bhatia333
6 Replies

10. UNIX for Dummies Questions & Answers

urgent sed help

Hi folks. I've tried to research this, but haven't found a good answer (could be my harried state). At any rate, I have records that end with two commas, a number, two commas this could be anywhere from ,,01,, to ,,09875953,, I need to remove the last two fields (the number... (3 Replies)
Discussion started by: kristy
3 Replies
Login or Register to Ask a Question