Sunday, May 5, 2024
15
rated 0 times [  15] [ 0]  / answers: 1 / hits: 29640  / 3 Years ago, mon, june 14, 2021, 3:55:14

My string look like this..



foo<.............................. 
bar</............................


I want to pipe for output as



foo
bar


Delete all characters after a first < found each line.


More From » command-line

 Answers
0

This is basic sed. Using sed is not difficult once you know regular expressions. A basic sed command for reading the input and stripping every < and the following part if it exist, then printing the line (may be modified):



$ echo 'foo<....' | sed 's/<.*//'
foo


sed uses regular expressions, the relevant manual page text for sed(1) that applies to the above command:




s/regexp/replacement/

Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes 1 through 9 to refer to the corresponding matching sub-expressions in the regexp




Alternative using cut (manual page for cut(1)), "split the string by < and take the 1st field.



echo 'foo<....' | cut -d'<' -f1


Alternative using grep, "match only everything containing characters of the set a to z (case insensitive)" (manual page of grep(1)):



echo 'foo<....' | grep -io '[a-z]*'


(note: I took the liberty to use [a-z]*, meaning "zero or more occurrences of a letter", because grep won't return an empty line when using the -o option)



Alternative using awk, using the same idea of cut (manual page of awk(1)):



echo 'foo<....' | awk -F '<' '{print $1}'

[#33527] Monday, June 14, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ionodest

Total Points: 252
Total Questions: 122
Total Answers: 100

Location: Liechtenstein
Member since Tue, Apr 27, 2021
3 Years ago
ionodest questions
Sat, Jan 1, 22, 06:31, 2 Years ago
Wed, May 12, 21, 03:21, 3 Years ago
Tue, Feb 22, 22, 09:46, 2 Years ago
Thu, Jun 30, 22, 12:12, 2 Years ago
;