1

I need to append the following list:

urack-L-
urack-L-
urack-L-
urack-L-
urack-L-

with urack-L-

urack-L-100
urack-L-101
urack-L-100
urack-L-101
urack-L-100  

and so on...

I haven't been able to find a method other than incremental (100,101,102, etc...) Any short and sweet sed tricks?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Mike
  • 21
  • 1

4 Answers4

4

awk:

awk '{printf "%s%d\n", $0, 101 - NR % 2}' file

The NR variable holds the current record number, by default the current line number. NR % 2 evaluates to 0 or 1 for even or odd numbered lines.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • While this code may answer the question, providing additional context regarding **how** and **why** it solves the problem would improve the answer's long-term value. – Alexander Jun 09 '18 at 05:15
  • @Alexander you are completely wrong. Please don't clutter up the site commenting on posts you don't understand. Thanks. – Ed Morton Jun 09 '18 at 12:23
  • 2
    Ed, that's what comments are for, too ask questions about stuff that's hard to understand – glenn jackman Jun 09 '18 at 14:59
  • Yeah I know but I also know that this guy has zero interest in the question or your answer and only posted his comment because some automated tool flagged your answer as possibly low quality since it was just code, and so he rubber-stamped it without having any clue if it was really low quality or not. Code only does not equal low quality, the tool is simply asking **people who know the domain** to make a determination, and tiny trivial scripts are best left for people to look up the man page to explain if they don't understand them. – Ed Morton Jun 09 '18 at 16:36
2

If you have GNU sed, you can use an extension to addresses:

sed '1~2s/$/100/;2~2s/$/101/' infile

where the address first~step matches the line with address first and then every stepth line.

Or, as pointed out in ctac's comment:

sed 's/$/100/;n;s/$/101/' infile

This uses the n command to print the pattern space (containing an odd line) and loading the next (even) line into it. This should work with any POSIX-conformant sed.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
1
 :%s/$/\=(line(".")%2==0?"101":"100")/


 % ................ whole file
 s ................ substitute 
 $ ................ end of line
 \=(line(".") ..... gets the number of the current line
 %2==0? ........... if the rest of division equals zero

Reference link.

SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
0

This isn't appropriate for sed. Just use a shell loop with a variable that you test.

i=100
while read line
do
    echo "$line$i"
    if [[ i == 100 ]]
    then i=101
    else i=100
    fi
done < inputfile > outputfile
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Shorter: `i=$(( i == 100 ? 101 : 100 ))` – Benjamin W. Jun 08 '18 at 22:10
  • Shorter still `i=10$((++i%2))` but in any case see [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice) for some of the issues with this and do it with the right tool for the job, awk (see https://stackoverflow.com/a/50769618/1745001). – Ed Morton Jun 09 '18 at 12:25