J.Clark
J.Clark

Reputation: 175

Adding characters to the end of a line using SED

I am trying to add there exclamation points to the end of each line in this text file. I was told I need to switch all characters from lower case to upper case and have done so with string below. I am not sure how to incorporate the exclamation points in the same sed statement.

cat "trump.txt" | sed 's/./\U&/g'

Upvotes: 2

Views: 5077

Answers (2)

P....
P....

Reputation: 18351

Here is awk version , where all the text will be converted into uppercase and then three exclamations would be added.

 awk '{$0=toupper($0) "!!!"}1' input
 THIS IS A SAMPLE TEXT!!!
 ANOTHER LINE!!!
 3 APPLES!!!

Explanation: $0 is entire line or record. toupper is an awk inbuilt function to convert input to uppercase. Here $0 is provided as input to toupper function. So, it will convert $0 to uppercase.finally uppercased $0 and !!! would be substituted to $0 as new values.

Breakdown of the command:

awk '{$0=toupper($0)}1' input # to make text uppercase.  
awk '{$0= $0 "!!!"}1' input   # to add "`!!!`" in the end of text.

Or bash way: ^^ sign after variable name will make contents of variable uppercase.

while read line;
   do
   echo "${line^^}"'!!!' ;
done <input

Upvotes: 1

Sundeep
Sundeep

Reputation: 23667

Consider this sample text:

$ cat ip.txt 
this is a sample text
Another line
3 apples

With sed command given in question which uppercases one character at a time with g flag

$ sed 's/./\U&/g' ip.txt 
THIS IS A SAMPLE TEXT
ANOTHER LINE
3 APPLES

To add some other characters at end

$ sed 's/.*/\U&!!!/' ip.txt 
THIS IS A SAMPLE TEXT!!!
ANOTHER LINE!!!
3 APPLES!!!
  • .* will match entire line and & will contain entire line while replacing
  • g flag is not needed as substitution is happening only once

Upvotes: 3

Related Questions