swam
swam

Reputation: 303

Print first letter of each word in a line

I have searched through other posts and have not found an answer that fits my needs. I have a file that is space delimited. I would like to print the first letter of each word in the given line. For example:

cat test.txt
This is a test sentence.

Using either sed, awk, or a combination, I would like the output to be "Tiats". Any advice on pointing me in the right direction?

Upvotes: 5

Views: 9714

Answers (11)

Victoria Stuart
Victoria Stuart

Reputation: 5082

Ah, surprisingly difficult task until I found this thread. ... I wanted to extract the first letter in a string of words. This worked:

echo 'Apple banana Carrot fruit-cake (Grapes)' | sed -r 's/.*/\L&/; s/-/ /g; s/[()]//g; s/(.)[^ ]* */\1/g'
abcfcg

i.e.

sed -r 's/.*/\L&/; s/-/ /g; s/[()]//g; s/(.)[^ ]* */\1/g'
  • \L& lowercases the string (to uppercase, use: \U&)
  • replace - with space
  • remove parentheses ()
  • last expression per other answers here, notably @arjun-mathew-dan
    • look for any character: (.)
    • followed by a sequence of non-space characters: [^ ]*
    • followed by optional space: *
    • replace this pattern with the first character [matched by (.) ]: \1

Upvotes: 1

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed 's/\B.\|[[:space:][:punct:]]//g' file

Delete all characters following the beginning of a word, spaces and punctuation.

Upvotes: 0

gniourf_gniourf
gniourf_gniourf

Reputation: 46833

A funny pure Bash solution:

while read -r line; do
    read -r -d '' -a ary <<< "$line"
    printf '%c' "${ary[@]}" $'\n'
done < text.txt

Upvotes: 2

dfeuer
dfeuer

Reputation: 48591

In Haskell, on one line:

main = putStr =<< (unlines . map (map head . words) . lines <$> getContents)

Perhaps more readably:

main = do
  line <- getLine  --Read a single line from stdin
  let allWords = words line --Turn the line into a list of words
  let firsts = map head allWords --Get the first letter of each word
  putStrLn firsts --Print them out
  main --Start over

Upvotes: 0

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed 's/ *\([^ ]\)[^ ]\{1,\} */\1/g' YourFile

Take directly all space length and place. Assuming just that space are the space character and not a tab (but could easily be adapted for)

just for fun

sed 's/ *\(\([^ ]\)\)\{1,\} */\2/g' YourFile

take the last letter instead of first

Upvotes: 0

Jotne
Jotne

Reputation: 41456

Another awk

awk '{for (i=1;i<=NF;i++) $i=substr($i,1,1)}1' OFS= file

This loops trough every word and cut off all except first letter.

Eks:

cat file
This is a test sentence.
Ahggsh Mathsh Dansdjksj

awk '{for (i=1;i<=NF;i++) $i=substr($i,1,1)}1' OFS= file
Tiats
AMD

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

Another perl command.

$ echo 'This is a test sentence.' | perl -nE 'print for m/(?<!\S)\S/g;print "\n"'
Tiats

Upvotes: 0

anishsane
anishsane

Reputation: 20980

Using perl:

$ echo This is a test sentence | perl -nE 'print for /^\w|(?<=\W)./g'
Tiats

Explanation: Print any non-white-space character, which is beginning of the line, or preceded by a white-space.

Upvotes: 0

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

Another solution with sed:

sed 's/\(.\)[^ ]* */\1/g' File

Here, we look for any character(.) followed by a sequence of non-space characters([^ ]*) followed by optional space( *). Replace this pattern with the first character(character matched by .).

Sample:

$ cat File
This is a test sentence.
Ahggsh Mathsh Dansdjksj
$ sed 's/\(.\)[^ ]* */\1/g' File
Tiats
AMD

Upvotes: 7

paxdiablo
paxdiablo

Reputation: 881563

One possibility:

pax> echo 'This is a test sentence.
  This is another.' | sed -e 's/$/ /' -e 's/\([^ ]\)[^ ]* /\1/g' -e 's/^ *//'
Tiats
Tia

The first sed command simply ensures there's a space at the end of each line to simplify the second command.

The second command will strip all subsequent letters and the trailing spaces from each word. A word in this sense is defined as any group of non-space characters.

The third is something added to ensure leading spaces on each line are removed.

Upvotes: 7

qwwqwwq
qwwqwwq

Reputation: 7309

In awk:

awk '{
  for (i=1; i<=NF; i++) {
    printf(substr($i, 1, 1));
  }
  printf("\n");
}' input_file

awk automagically sets NF to be the number of fields in the line, loop through each one and use substr to get the first letter

Upvotes: 2

Related Questions