Remove prefix using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove prefix using awk
# 1  
Old 06-01-2009
Remove prefix using awk

Remove prefix using awk

File:
Quote:
xyz.amp|x|a|t
xyz.tmpx|a|t
aic.klmx|a|t
jil.djn|a|t
mno.djn|j|k
dij.iuy|a|t
Code:
 nawk -F"|" '{if ($1 ~ /^xyz./) print; else { gsub(.*\..*, \..*, $1) ;print }}' file

Error:
Quote:
nawk: syntax error at source line 1
context is
{if ($1 ~ /^xyz./) print; else { >>> gsub(. <<< *\..*, \..*, $1) ;print}}

ouput required:
Quote:
xyz.amp|x|a|t
xyz.tmpx|a|t
klmx|a|t
djn|a|t
djn|j|k
iuy|a|t
# 2  
Old 06-01-2009
check the syntax of your gsub().
# 3  
Old 06-01-2009
how about below shell?

Code:
while read line;do
set $line
echo ${1#*.}
done < yourfile


Last edited by vidyadhar85; 06-01-2009 at 11:59 PM..
# 4  
Old 06-02-2009
Quote:
Originally Posted by summer_cherry
how about below shell?

Code:
while read line;do
set $line
echo ${1#*.}
done < yourfile

check your output.
# 5  
Old 06-02-2009
Code:
sed -n '/^xyz\./p;/^xyz\./!{s/^[^.|]*\.\(.*\)/\1/p}' filename

op:
HTML Code:
xyz.amp|x|a|t
xyz.tmpx|a|t
klmx|a|t
djn|a|t
djn|j|k
iuy|a|t
Or using awk:

Code:
awk -F "|" '/^xyz\./{print;next}{sub(/^[^.]*\./,"",$1);print}' OFS="|" filename

HTML Code:
xyz.amp|x|a|t
xyz.tmpx|a|t
klmx|a|t
djn|a|t
djn|j|k
iuy|a|t
-Devaraj Takhellambam

Last edited by devtakh; 06-02-2009 at 01:40 AM.. Reason: added awk solution also
# 6  
Old 06-02-2009
Or shorter:

Code:
awk '!/xyz/{sub(".*\.","")}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk move select fields to match file prefix in two directories

In the awk below I am trying to use the file1 as a match to file2. In file2 the contents of $5,&6,and $7 (always tab-delimited) and are copied to the output under the header Quality metrics. The below executes but the output is empty. I have added comments to help and show my thinking. Thank you... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Need to add prefix using sed or awk from cat the file

I need the use sed or AWK using cat the file Node1 TDEV RW 1035788 TDEV RW 1035788 Server1 TDEV RW 69053 Server2 TDEV RW 69053 TDEV RW 103579 Server3 TDEV RW 69053 server4 RDF1+TDEV RW 69053 RDF1+TDEV RW 517894 RDF1+TDEV RW 621473 server6 TDEV RW 34526 TDEV RW 34526 (22 Replies)
Discussion started by: ranjancom2000
22 Replies

3. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. Shell Programming and Scripting

awk to remove last two -*-*

fq-bar-something-1.0-r1.src.rpm ----> fq-bar-something fq-bar-xx-r1-rel.src.rpm ---------> fq-bar-xx fq-bar-ff-ver-11-rel.src.rpm -------> fq-bar-ff-ver any help? (5 Replies)
Discussion started by: yanglei_fage
5 Replies

5. Shell Programming and Scripting

AWK adding prefix/suffix to list of strings

75 103 131 133 138 183 197 221 232 234 248 256 286 342 368 389 463 499 524 538 (5 Replies)
Discussion started by: chrisjorg
5 Replies

6. Linux

Adding a prefix to a column using awk/sed commands

Hello, I am a newbie to linux and struggling to find a better way to append a column in a text file. Here is the file i want to modify: It has 8 columns (and thousands of rows). I want to append the first column by adding "chr" infront of the numbers. Some rows have a string in the first... (4 Replies)
Discussion started by: bjorngill
4 Replies

7. Shell Programming and Scripting

Remove prefix per line in file

Hi, I'm using a .ksh script to split one file into multible files by checking for the prefix per line. It works perfekt (thanks again for anyone involved in helping me with that ;)), but I want to remove the prefix per line too. Means only the line information itself should remain in the... (7 Replies)
Discussion started by: spidermike
7 Replies

8. Shell Programming and Scripting

Need awk script to add a prefix to each line in file

Hello , I have file with below content : '165567885', '165568443', '165568805', I need an awk script that would add a prefix zero after first ' . Like '0165567885', '0165568443', '0165568805', Please help. Thanks in advance. (5 Replies)
Discussion started by: rmv
5 Replies

9. Shell Programming and Scripting

Remove prefix from filenames

I'm trying to put together a shell script that will append specific prefixes based on the content of filenames. I think I have this part down. However, I want to append before that part a process that will remove the current prefix before it renames the files with the new prefix. For example,... (6 Replies)
Discussion started by: HLee1981
6 Replies

10. UNIX for Dummies Questions & Answers

remove filename prefix

I've got a bunch of files called oldabc, olddef etc. i want to copy these to be abc, def.... I can do this with file extensions....but can get the logic to work for prefixes. All the files I am interested in have a prefix of 'old'. This loop is no good for me....it looks at the content... (2 Replies)
Discussion started by: peter.herlihy
2 Replies
Login or Register to Ask a Question