Using sed in awk column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed in awk column
# 1  
Old 08-21-2015
Using sed in awk column

Hello,

I need to translate a file with more thousand lines like this
Code:
zoneobjectrename "AAA_to_BBB"

in a lines
Code:
zoneobjectrename "AAA_to_BBB","AAA_BBB"

I tried to using awk and sed in this manner but it don't work
Code:
grep _to_ Documenti/zoneSWITCH2 |awk '{print "zoneobjectrename " $2 "," $2= sed 's/_to_/_/' $2}'

Thanks for the help


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 08-21-2015 at 10:18 AM.. Reason: Added CODE tags
# 2  
Old 08-21-2015
Hello Alen912,

Following may help you in same.
Code:
 awk -F"\"" '{print $1 OFS $2 OFS}' OFS="\""  Input_file

Output will be as follows.
Code:
 zoneobjectrename "AAA_to_BBB"

Thanks,
R. Singh
# 3  
Old 08-21-2015
Thanks RavinderSingh13,
I need to add another columns with the content of $2 but changing _to_ with _.
Regards

Last edited by rbatte1; 08-21-2015 at 10:19 AM.. Reason: Added ICODE tags
# 4  
Old 08-21-2015
Please use code tags as required by forum rules!

Try
Code:
awk '{X=$2; sub(/_to/,"",X);$2=$2 "," X}1' file
zoneobjectrename "AAA_to_BBB","AAA_BBB"

# 5  
Old 08-21-2015
Or for a sed solution:
Code:
sed -e 's/\(\("[^_]*\)_to_\([^"]*"\)\)$/\1,\2_\3/' file
zoneobjectrename "AAA_to_BBB","AAA_BBB"

Or a shell solution:
Code:
while read key word; do echo $key $word,${word%%_*}_${word##*_}; done < file

# 6  
Old 08-21-2015
or, if you prefer perl:

Code:
$ perl -e '{while (<>) {chomp;print;print " ".(join ("_",(split "_to_",(split " ",$_)[1])));print "\n"}}' <input.txt
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"
zoneobjectrename "AAA_to_BBB" "AAA_BBB"

for the obvious input file.
# 7  
Old 08-21-2015
Code:
$ cat alen192.file 
Do not change this line
Change this line zoneobjectrename "AAA_to_BBB"
Do not change this line
zoneobjectrename "AAA_to_BBB"
Do not change this line nor the next
Zoneobjectrename "AAA_to_BBB"

$ perl -pe 's/zoneobjectrename (.{4})_to_(.{4})/$&,$1_$2/' alen192.file 
Do not change this line
Change this line zoneobjectrename "AAA_to_BBB","AAA_BBB"
Do not change this line
zoneobjectrename "AAA_to_BBB","AAA_BBB"
Do not change this line nor the next
Zoneobjectrename "AAA_to_BBB"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

4. UNIX for Dummies Questions & Answers

Using awk/sed to determine whether there are any 2's, 3's and 4's in a column

Hi, I have a very large file which is of the general format: 0 3 4 2 ... 3 2 4 0 ... 0 3 4 2 ... 3 0 4 2 ... . . . . ... . . . . ... I would like to apply a simple awk/sed script to work out whether there are any 2's, 3's and 4's in each column. The results would look as... (3 Replies)
Discussion started by: kasan0
3 Replies

5. Shell Programming and Scripting

use sed on only column $3 with awk

I am using the command ls -lhB --group-directories-first --no-group --file-type --time-style=+%D $@ | sed 's/AlphaLexman/ALM/g' to replace my username with my initials (to keep the width shorter), but if my username is in the filename such asreport_by_AlphaLexman.txtit becomesreport_by_ALM.txt ... (2 Replies)
Discussion started by: AlphaLexman
2 Replies

6. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

7. Shell Programming and Scripting

Deleting the first column with sed,awk or perl

336 brtr 256 hello Output: brtr hello How can i do this with sed,awk or perl? (5 Replies)
Discussion started by: cola
5 Replies

8. Shell Programming and Scripting

Remove certain parameters from column using awk or sed

I have a text file Nov 1 LOG_10_000000343.gzip_COMPLETE 2910 server.log.3 Nov 4 LOG_10_000000343.gzip_COMPLETE 2910 server.log.4 Dec 5 LOG_10_000000343.gzip_blah 2910 server.log.5 Jul 6 LOG_10_000000343.gzip_ERROR 2910 server.log.1 I need to convert this to Nov 1 LOG_10_000000343.gzip... (3 Replies)
Discussion started by: gubbu
3 Replies

9. Shell Programming and Scripting

sed/awk-adding numeric to a column

I have a txt file as follows Code: Oct 1 file1 4144 Oct 1 file23 5170 Oct 2 file5 3434 Oct 21 file56 2343 I need to add a new column by marking the right log file from current directory. For example populate like this. Please not in the second columt for "1" it has... (2 Replies)
Discussion started by: gubbu
2 Replies

10. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies
Login or Register to Ask a Question