2

How do I append a string that has variables in it?

sed 's/MIMG109AL1ARP\t[[:alnum:]]*\t/ /g'

I initially thought sed, but I don't want to replace anything, I want to add something after the intial line.

Some example strings are as follows:

MIMG109AL1ARP     PQHDO0542I7537

MIMG109AL1ARP     PQHDO0372I2435

MIMG109AL1LLL     QRHDO0382I2342

MIMG109AL1LLL     PQHDO362I2345

However I want them to look like this:

MIMG109AL1ARP     PQHDO0542I7537     appendagehere

MIMG109AL1ARP     PQHDO0372I2435     appendagehere

MIMG109AL1LLL     QRHDO0382I2342     appendage2here

MIMG109AL1LLL     PQHDO362I2345      appendage2here

Just edited the file to make my question more clear! I need to append separate lines separately, not every line. Thank you for your input so far. Any help or redirection is appreciated!

7
  • awk '$1~/^[A-Z]/ {print $0 "\tappendagehere"}' file... Or, if you want sed: sed '/^[A-Z]/ s/$/\tappendagehere/' file
    – jasonwryan
    Nov 18, 2015 at 7:19
  • maybe use vi and learn to love the A (append to end-of-line) and . (repeat-last-command) commands. for a few dozen or a few hundred lines it's often easier and quicker to edit manually with a decent editor (such as vi).
    – cas
    Nov 18, 2015 at 7:51
  • ah I would but there is over 1000 lines I would need to edit unfortunately, and it's a script I wanted to create because it's a command I would need to run a couple times a year :( Nov 18, 2015 at 7:53
  • But I think you guys really got me on the right track so far. When I finish troubleshooting I will make sure to upvote the answer :) Nov 18, 2015 at 7:54
  • of those 1000+ lines, how many different matches are there? and how many different strings need to be appended?
    – cas
    Nov 18, 2015 at 10:06

3 Answers 3

1

In sed, $ represents the end of the line. So you can say

sed -e 's/$/\twhatever/'

If you want to append only to non-empty lines, but leave empty lines empty, then use the following (where & in sed is the matched string, which in this case is one character):

sed -e 's/.$/&\twhatever/'

Or you can also just only do the substitution on non-empty lines:

sed -e '/./s/$/\twhatever/'
8
  • This breaks on empty lines...
    – jasonwryan
    Nov 18, 2015 at 7:23
  • it doesn't exactly break on empty lines, it just applies to them too. if you don't want to append \t whatever to empty lines, use sed -e 's/^.\+$/&\twhatever/' instead. or sed -e 's/^[A-Z0-9a-z]\+[[:space:]]\+[A-Z0-0a=z]\+$/&\twhatever/'
    – cas
    Nov 18, 2015 at 7:26
  • @cas I'll rephrase to accomodate your baseless nitpicking: "it produces unwanted output on emtpy lines, which in the real world is functionally equivalent to breakage."
    – jasonwryan
    Nov 18, 2015 at 7:29
  • it's not up to you to decide if it's "unwanted output". in some cases, that may be exactly what is wanted. saying that that is "broken" is, well, broken.
    – cas
    Nov 18, 2015 at 7:31
  • @jasonwryan: I didn't realize you had empty lines in your example, but I guess your transcript does. See my revised answer, which has two ways to do what you want. Nov 18, 2015 at 7:33
1

With some help from one of my friends, I was able to get it to work using this line!

sed 's/\(MIMG109AL1ARP..*\)\($\)/\1\twhatIwant\2/g'

The solution was to break up the expression into two sub expressions. Instead of changing what I already had, I was able to flank what I wanted between the two sub-expressions.

Thank you both for posting and trying to help me out.

2
  • 1
    if you considered answers helpful, you should flag them as such. Nov 18, 2015 at 12:23
  • Thanks, I did, but because I'm a new user with less than 15 rep, they don't show up yet! Nov 19, 2015 at 2:19
0

With only 9 different patterns and appendages to deal with, try something like this:

#! /usr/bin/perl

use strict;

my %append = ( # matchregex       => appendstring
               qr/^MIMG109AL1ARP/ => 'appendagehere',
               qr/^MIMG109AL1LLL/ => 'appendage2here',
               # more here
             );

while (<>) {
   chomp;
   foreach my $re (keys %append) {
       if (m/$re/) {
           s/$/\t$append{$re}/;
           last;
       }
   }
   print $_, "\n";
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .