How do i replace a word ending with "key" using awk excpet for one word?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i replace a word ending with "key" using awk excpet for one word?
# 1  
Old 07-20-2015
How do i replace a word ending with "key" using awk excpet for one word?

Code:
echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | 
awk 'gsub(/^[grp_key,]|abc,|[key;]$/,"") {print}'

Required output
Code:
{grp_key,xyz,aaa,ccc}

# 2  
Old 07-20-2015
Try
Code:
echo mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc | awk '/grp_key/ {print;next}; /abc|key/ {next}1' RS=, ORS=,
grp_key,xyz,aaa,ccc

The braces cause brace expansion in bash; if needed, additional action must be taken.
# 3  
Old 07-20-2015
How the input looks like? Is it CSV as you shown? located where? from file , string or something else?
# 4  
Old 07-20-2015
Hi clx ,
You could say it is a string it is not CSV file.

RudiC
Thanks i will try that , i forgot to add i am using korn shell.

Thanks
# 5  
Old 07-20-2015
Hello 100bees,

Following may help you in same too.
Code:
 echo mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc | awk -F, '{for(i=1;i<=NF;i++){if($i ~ /grp_key/){A=A?A OFS $i:$i};if($i !~ /key/){A=A?A OFS $i:$i}}} END{print A}' OFS=,

Output will be as follows.
Code:
 grp_key,abc,xyz,aaa,ccc

Suggestion provided by RudiC is great but it will give at last a comma as ORS=, so above may fix that.


Thanks,
R. Singh
# 6  
Old 07-20-2015
Code:
% echo "{mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc}" | perl -pe 's/\w+(?<!grp)_key,|abc,//g'
{grp_key,xyz,aaa,ccc}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add the word "prefix" to beginning of line using sed

SUSE linux bash shell this works test -d /tmpp && echo "directory exists" || echo "directory doesn't exists" |sed -e "s/^/prefix /" prefix directory doesn't exists but why doesn't this work? test -d /tmp && echo "directory exists" || echo "directory doesn't exists" |sed -e... (3 Replies)
Discussion started by: snoman1
3 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. UNIX for Dummies Questions & Answers

How to get last word from a line ending with "/" in Unix

Hello All, I have a scenario to read a file containing text like this:(say file name is Dummy.txt) /home/abc/test1/ | file1 /home/abc/test2/ | file2 I used a variable to store the content from file like this (say for line1): File=`head -1 Dummy.txt | cut -f1 -d "|"` Dir=`head -1 Dummy.txt... (2 Replies)
Discussion started by: Quesemail
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Interesting question - Search and replace the word after sign "="

Hi Guys, Req your help in searching and replacing the word that comes after equals(=) symbol I would like to replace the sting in bold with a string in variable. d=ABCDF8C44C22 # grep -i NIM_MASTERID ${_NIMINFO} export NIM_MASTERID=00CDF8C44C00 I'm looking to replace any word that... (4 Replies)
Discussion started by: ajilesh
4 Replies

6. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

7. Shell Programming and Scripting

Extract Part of a "Word", using AWK or SED????

I have been lurking on this forum for some time now and appreciate Everyone's help. I need to find a way to get the SystemID from this XML file. The file is much larger than just this one line but I can grep and get this line Printed. But really just need the "systemid". <test123: prefintem... (9 Replies)
Discussion started by: elbombillo
9 Replies

8. Shell Programming and Scripting

cat/delete per line any word "192.168.1.12"

Hi All Can u help me.. My problem is delete word per line sample: cat /tmp/file.txt monitor 192.168.1.11 Copying files in current directory 1 monitor 192.168.1.1 Copying files in current directory 2 monitor 192.168.1.12 Copying files in current directory 3 monitor 192.168.1.14... (1 Reply)
Discussion started by: carnegiex
1 Replies

9. Shell Programming and Scripting

replace word with using "sed" not work...

Hi, I have a xml text file with the following data, I would like replace F0</Number> to F</Number> only. i used sed to replace, but it not work!! anyone can help? <Number>11 20 03 22 23 21 91 00 F0</Number> <Number>12 20 03 20 99 21 91 20 F0</Number> <Number>10 21 03 21 78 21 92 27... (28 Replies)
Discussion started by: happyv
28 Replies
Login or Register to Ask a Question