I have a file that contains the following information:

Username: Sjohhny@email1.com
Value one: xx:xx:xx:xx:xx:xx
Value two: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
Value three: Sjohhny@email1.com@[xx:xx] [a mixuture of the three values]

Username: Vsamba@email3.com
Value one: mm:mm:mm:mm:mm:mm
Value two: mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm
Value three: Vsamba@email3.com@[xx:xx] [a mixuture of the three values]

Username: Skrids@email2.com
Value one: yy:yy:yy:yy:yy:yy
Value two: yy:yy:yy:yy:yy:yy:yy:yy:yy:yy:yy:yy:yy:yy:yy
Value three: Skrids@email2.com@[xx:xx] [a mixuture of the three values]

Username: Vconan@email4.com
Value one: zz:zz:zz:zz:zz:zz
Value two: zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz
Value three: Vconan@email4.com@[xx:xx] [a mixuture of the three values]

First, I want to filter out all the information of the usernames that start with S and keep those that start with V. After that, I want to make an end product that is a .sh script that I can run. What I want to do is:

  1. Echo the username
  2. Delete the third value
  3. Run the command Hello with the switches -One, Two, and --Thanks -for -visiting on the first and second values.

So, my .sh file will be similar to this

echo Username: Vsamba@email3.com
Hello -One mm:mm:mm:mm:mm:mm -Two: mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm --Thanks -for -visiting

echo Username: Vconan@email4.com
Hello One zz:zz:zz:zz:zz:zz -Two: zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz --Thanks -for -visiting

I watched a whole video series on Youtube regarding Sed and read many of the articles here trying to come up with a script that will result in the script I wanted, but it all failed. Could you please show me how to write such a script? I would also love it if you recommend websites/refrences that are readable by new users regarding sed (or Awk or Perl)

Please note that the number of ":" is static for value one and value two, this might be used somehow. I tried to use it but everytime I got both values when I asked only for value one, but you know how to utilize such fact much better than me.

Sorry for the long question! It just take tooooo much time to do all this without automating the process and I am totally in the linux world (and computer in general, for that matter!)

2 Answers 2


Since your file is structured, with blank lines between records, I'd suggest using awk or perl in paragraph mode. For example, using

  • one or more blank lines as record separator RS=
  • colon-space or newline as field separators -F': |\n'

and then printing the desired information if field $2 starts with V:

awk -vRS= -F': |\n' '
  $2 ~ /^V/ {
    print "echo " $1 ": " $2; 
    print "Hello -One " $4, "-Two " $6 " --Thanks -for -visiting"; 
    print "";
  }' file


echo Username: Vsamba@email3.com
Hello -One mm:mm:mm:mm:mm:mm -Two mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm:mm --Thanks -for -visiting

echo Username: Vconan@email4.com
Hello -One zz:zz:zz:zz:zz:zz -Two zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz:zz --Thanks -for -visiting

You could use:

sed -rn '/Username: S/,/^$/d;s/Username/echo &/p;/one: /N;s/Value one: (.+)\n/Hello -One: \1/;s/Value two: (.+)/ -Two \1 --Thanks -for -visiting\n/p' file

Or more readably

sed -rn '{
        /Username: S/,/^$/d
        s/Username/echo &/p
        /one: /N
        s/Value one: (.+)\n/Hello -One: \1/
        s/Value two: (.+)/ -Two \1 --Thanks -for -visiting\n/p
}' file | tee newfile


  • -r use extended regular expressions so we don't have to escape () or +
  • -n don't print anything until we ask for it (this is how we will get rid of the third line)
  • /Username: S/,/^$/d find Username: S, read from there until an empty line, and delete all of that
  • s/thing/&/ find thing and replace it with itself
  • /one: /N find one: and read the next line too so we can join them
  • s/Value one: (.+)\n/ match Value one: and whatever characters come after it up to the newline (\n) which we need to remove to join the lines, and save the unknown characters
  • \1 backreference to the pattern saved with ()

  • p print this line

(I have learned some of my sed-fu (I'm just a beginner) here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.