Using sed or awk?

What if I wanted to add a word such as IT after the first character and if theres 3 characters, after the 2nd character?

output would be:

G, it H
G, H it P
G, H, P it L

I'm thinking that AWK would be the easiest way to do this... Currently looking it up.

Right now I'm using awk but I can only replace every "," with "it". I dont know how to replace the last "," with the word "it"

My output looks like this :

G it H it
G it H it P it
G it H it P it L it

I just figured out how to target my "," but I dont know how to add in "it"

What is you input?

I'd do it this way:

awk '{ sub( ",$", " it", $(NF-1) ); print}'

Adds "it" before the last token on the line.

my input is "H","T","Y","P","L" ( increases based off input of user )
Im trying to add a word here "H","T","Y","P"HERE"L"

---------- Post updated at 08:30 PM ---------- Previous update was at 08:25 PM ----------

Using your code, I cant get it to be withing the 2nd to last quotes.
However I am really close!

Yes, my solution was based on different input. Try this:

awk  -F "," -v OFS="," '{ x = $(NF-1) " it " $NF; NF -=2; ; print $0, x }'

Creates a new 'token' with the last and next to last tokens placing 'it' in between. Shortens the original input line by two tokens, then prints the short string with the synthetic value in x.

sed 's/,\([^,]*\)$/HERE\1/'

sed might be slightly simpler for this one:

$ echo '"H","T","Y","P","L"' | sed 's/,\([^,]*\)$/ it \1/'
"H","T","Y","P" it "L"
$ 

Edit: LOL Scrutinizer, great minds work alike

sed 's/\(.*\),/\1it/'

My sed may not always be as terse as the awk from awk gurus, but you might figure out what it does and how it does it more easily. The wild card .* is a greedy spring anchored by the last comma it has to precede. The substring grabber \(\) lets you put the fron of the line back as \1. The end of the line floats after it.

The opposite way is this:

sed 's/,\([^,]*\)$/\1/'

It puts a spring at end of line that captures all not comma characters. This sort of not X wildcard is very useful, as it stops short of the next sort of thing. To me, the charm of sed is the reuse and generality of the tools. I tends to perform faster, too.

1 Like

Hi, DGPickett, you probably mean:

sed 's/\(.*\),/\1 it /'

nice.

1 Like

Oh, he wanted spaces, well, some enhancements are left to the student! I guess our look and post cycles crossed, so like a pair of bad script processes! :smiley:

1 Like

Hi, I meant the closing / actually ;). Have you discovered the "[code] tags" button already?

1 Like

Yes, but sometimes it is so trivial that I skip it. The source gets ugly and the output grows linefeeds or boxes. There, the dot there was a miss for the slash; revisionist history!

---------- Post updated at 09:48 PM ---------- Previous update was at 09:43 PM ----------

I started regex with MULTICS qedx in 1969, often on a printing 300 baud terminal, and then Waterloo Fred under GCOS TSS, before I first saw UNIX in 1990.

I am starting to like sed more and more by the minute :stuck_out_tongue:

Pipes and sed means never having to say "sorry about using all the temp disk space", "the file is too large for my editor" or "please remove your temp file from testing so I can write one", not to mention "what are all these CPUs for?" or "It is not done yet, so I do not know what the output will look like."

Modern sed implementations have no buffer limit, so I have taken whole pages and swapped the line feeds and form feeds back and forth using tr, so I could put pages into insert statements and then into a command line database tool.