iNoob
iNoob

Reputation: 1395

Advice manipulating text files using sed and awk

Ive been playing with sed and awk but cant seem to get this working as expected. Im trying to take a textfile of firstname lastname perline and manipulate them to show firstname+lastname inital.

Ive achieved Last initial followed by firstname but cant get it the other way around ^_^ and help would be appreciated

cat /root/Desktop/Userlist.txt | awk '{ print $2, $1 }' | sed 's/\(.\).* /\1/g'

EXAMPLE of whats expected

John Smith
JohnS

Upvotes: 0

Views: 400

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/^\(.\)[^]* \{1,}\([^ ]\{1,}\).*/\2\1/' /root/Desktop/Userlist.txt 

posix compliant

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247012

awk

$ echo "John Smith" | awk '{printf "%s%s\n", $1, substr($2,1,1)}'
JohnS

Upvotes: 1

SzG
SzG

Reputation: 12629

sed -r 's/^([^ ]+) ([^ ]).*$/\1\2/' /root/Desktop/Userlist.txt

No cat. sed is able to take input filenames as arguments.

Upvotes: 4

Related Questions