How to append to specific lines in a flat file using shell script?

I'd do it this way sed '/\|23861\|/{s/$/|Something/;}' file This is similar to Marcelo's answer but doesn't require extended expressions and is, I think, a little cleaner. First, match lines having 23861 between pipes /\|23861\|/ Then, on those lines, replace the end-of-line with the string |Something {s/$/|Something/;} If you want to do more than one of these you could simply list them sed '/\|23861\|/{s/$/|Something/;};/\|30645\|/{s/$/|SomethingElse/;}' file.

This is indeed cleaner. However,I was ideally trying to modify inline inside the same file, without having to redirect the output into a different file (or back to the same file). Sed -i doesn't seem to work on my AIX box.

If there is no other option to do an inline replacement, I shall go with your answer. Thanks. – AKS Sep 14 at 14:11 @AKS: IF AIX's sed does not support -i, can you install gsed?

Or maybe it's already installed? – Sorpigal Sep 14 at 18:15 @Sorpigal: Agreed. I was trying to figure out how to address a line by regex.

The man page was no help. – Marcelo Cantos Sep 14 at 21:33.

Use the following awk-script: $ awk '/23861/ { $0=$0 "|Processed" } {print}' input 11|30646|654387|020751520 11|23861|876521|018277154|Processed 11|30645|765418|016658304 or, using sed: $ sed 's/\(.*23861. *$\)/\1|Processed/' input 11|30646|654387|020751520 11|23861|876521|018277154|Processed 11|30645|765418|016658304.

1 or awk 'BEGIN {OFS=FS="|"} /23861/ {$(NF+1) = "Processed"} {print}' – glenn jackman Sep 14 at 17:28.

Use the substitution command: sed -i~ -E 's/(\|23861\|. *)/\1|Processed/' flat. File (Note: the -i~ performs the substitution in-place.

Just leave it out if you don't want to modify the original file. ).

Thanks. In-line substitution is exactly what I'm looking for, but sed -i doesn't seem to be work on my AIX box. It gives: sed: Not a recognized flag: I Usage: sed -n -u Script File ... sed -n -u -e Script ... -f Script_file ... File ... Maybe this isn't supported on AIX – AKS Sep 14 at 14:07 sed -i doesn't really do anything in-place anyway; it just disguises a process of writing output to a temporary file which it then moves over the original, which is easily done by hand.

– flabdablet Oct 9 at 8:56 @flabdablet: Who cares about the mechanics? Sed -i~ ... abc is way more convenient — not to mention less error-prone — than sed ... abc > .abc. Tmp && mv abc abc~ && mv .abc.

Tmp abc. – Marcelo Cantos Oct 9 at 11:23.

You can use the shell while read -r line do case "$line" in *23681*) line="$line|Processed";; esac echo "$line" done tempo && mv tempo file.

Sed is just a stream version of ed, which has a similar command set but was designed to edit files in place (allegedly interactively, but you wouldn't want to use it that way unless all you had was one of these). Something like field_2_value=23861 appended_text='|processed' line_match_regex="^^|*|$field_2_value|" ed "$file" "$output_file" You need to be sure that the values you put in field_2_value and appended_text never contain slashes, because ed's g and s commands use those for delimiters. If they might do, and you're using bash or some other shell that allows ${name//search/replace} parameter expansion syntax, you could fix them up on the fly by substituting \/ for every / during expansion of those variables.

Because bash also uses / as a substitution delimiter and also uses \ as a character escape, this ends up looking horrible: appended_text='|n/a' ed "$file".

If temp. Txt is your file, try: awk '$0! ~ /NO/ {print $0 "|YES|"} $0 ~ /NO/ {print}' temp.txt.

Thanks. This works well :) If I didn't want to append YES to lines containing either NO or aaa, how can I modify the above command? – AKS Oct 12 at 16:42 1 You should use the '|' (alternative) regexp character, like this awk '$0!

~ /NO|aaa/ {print $0 "|YES|"} $0 ~ /NO|aaa/ {print}' temp. Txt As a matter of interest, what was the speed difference with your test case? – Max Oct 12 at 16:52 Super.

This is just what I want. The operation now takes under a minute to execute. My previous logic used to take about 3 hours :) Thanks a lot – AKS Oct 12 at 17:02 Glad to be of service... – Max Oct 12 at 17:05.

This will be quick: sed '/NO/! S/$/|YES|/' filename.

Simple with awk. Put the code below into a script and run it with awk -f script file > temp /\|NO\|/ { print; next; } # just print anything which contains |NO| and read next line { print $0 "|YES|"; } # For any other line (no pattern), print the line + |YES| I'm not sure about awk regexps; if it doesn't work, try to remove the two \ in the first pattern.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions