Remove First word of a sentence in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove First word of a sentence in shell
# 1  
Old 07-26-2015
Remove First word of a sentence in shell

Hi there,

How I remove the first word of a sentence.

I have tried.

Code:
echo '1.1;'  ;     echo "$one" | grep '1.1 '    | awk '{print substr($0,index($0," ")+1)}'

For the below input.
Code:
1.1 Solaris 10 8/07 s10s_u4wos_12b SPARC

Just want to know if there is any shorter alternative.
# 2  
Old 07-26-2015
Her is some solution. Some builtin and last one is using some external command, in this case cut.
Code:
# if using ksh:
x="1.1 2.2 3.3"
echo $x | read a b
echo "1:$a"

# all sh-shells
read a b c <<EOF
$(echo $x)
EOF
echo "2:$a"

Xdelim=" "
echo "3:${x%%${Xdelim}*}"

echo "4:"
echo $x | cut -d " " -f 1

# 3  
Old 07-26-2015
I must be missing something here...

Calling 1.1 Solaris 10 8/07 s10s_u4wos_12b SPARC a sentence is stretching the term sentence way beyond it meaning in English.

For the given input, the simple solution is:
Code:
printf '1.1;\nSolaris 10 8/07 s10s_u4wos_12b SPARC\n'

For the general case, there is never any reason to pipe the output of grep into awk; awk can easily do the work grep does without invoking both awk and grep.

Is there ever any reason to set a variable to a known string and then write commands to split that known string into substrings? If you know the components of your string; you know the substrings as well. Why not just set the substrings directly?

If your data is in a file, why are you splitting out lines from that file and echoing them into pipelines instead of just feeding the file directly into awk as an input operand?

This thread looks like someone is showing us a tree when we need to take a step backwards and look at the forest. What are you really trying to do here?
# 4  
Old 07-27-2015
Quote:
Originally Posted by alvinoo
.
.
.
Code:
echo '1.1;'  ;     echo "$one" | grep '1.1 '    | awk '{print substr($0,index($0," ")+1)}'

can't give
Quote:
Code:
1.1 Solaris 10 8/07 s10s_u4wos_12b SPARC

, the ";" is missing.
Wild guessing on your input variable, try
Code:
echo ${one/ /;}
1.1;Solaris 10 8/07 s10s_u4wos_12b SPARC

---------- Post updated at 11:57 ---------- Previous update was at 11:52 ----------

In case the one variable contains several lines, try
Code:
echo "$one" | awk '/1.1/ {$1=$1";"; print}'
1.1; Solaris 10 8/07 s10s_u4wos_12b SPARC

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

2. Shell Programming and Scripting

match sentence and word adn fetch similar words in alist

Hi all, I have ot match sentence list and word list anf fetch similar words in a separate file second file with 2 columns So I want the output shuld be 2 columns like this (3 Replies)
Discussion started by: manigrover
3 Replies

3. Shell Programming and Scripting

SED (or other) upper to lowercase, with first letter of first word in each sentence uppercase

The title pretty much defines the problem. I have text files that are all in caps. I would like to convert them to lowercase, but have the first letter of the first word in each sentence in uppercase. I already have SED on the server for fixing / tweaking text files, but I'm open to other... (5 Replies)
Discussion started by: dockline
5 Replies

4. Shell Programming and Scripting

Extract a word from sentence

$SET_PARAMS='-param Run_Type_Parm=Month -param Portfolio_Parm="997" -param From_Date_Parm="2011-08-09"' Want to extract the value of "Portfolio_Parm" from $SET_PARAMS i.e in the above case "997" & assigned to new variable. The existence order of "Portfolio"Parm" can change, but the name... (2 Replies)
Discussion started by: SujeethP
2 Replies

5. Shell Programming and Scripting

Trim the sentence containing colon and period to extract a word in between

Hello All , i am a newbie in korn shell scripting trying to trim a sentence that is parsed into a variable . The format of the sentence has three words that are separated from other by a " : " colon and "." period . Format of the sentence looks like ... (5 Replies)
Discussion started by: venu
5 Replies

6. Shell Programming and Scripting

How do you split a sentence after every nth word

Hi, I think my problem is a "simple" one to resolve. What i am looking for is a way in sed/awk to split a long line/paragraph into say 5 words per line. For example: Sentence/paragraph contains: 102 103 104 105 106 107 109 110 .... I would like the output to be (if splitting every 5... (5 Replies)
Discussion started by: muay_tb
5 Replies

7. Shell Programming and Scripting

Truncate the word from a sentence

Hi, The first line of a file is as follows: example.4ge v.45352 Report for April 28 May 2010 I need to remove the word example.4ge v.45353 from that line. I used the following command to truncate it sed 's/example.4ge v.45352//g' $filename But here the version number 45352 may... (4 Replies)
Discussion started by: Kattoor
4 Replies

8. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

9. Shell Programming and Scripting

How to remove duplicate sentence/string in perl?

Hi, I have two strings like this in an array: For example: @a=("Brain aging is associated with a progressive imbalance between intracellular concentration of Reactive Oxygen Species","Brain aging is associated with a progressive imbalance between intracellular concentration of Reactive... (9 Replies)
Discussion started by: vanitham
9 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question