0.1 C
New York
Saturday, December 7, 2024

Find out how to work with substrings on Linux



$ echo "Give attention to Peace on Earth" | minimize -d' ' -f3,5
Peace Earth
$ echo "one two three 4 5 6" | minimize -d' ' -f1-3,6
one two three 6

To make use of an alternate delimiter (on this case, a colon), use a command like this:

$ minimize -d':' -f1-3,5,6 /and so on/passwd | tail -n 5
justme:x:1004:JustMe:/dwelling/justme
lola:x:1006::/dwelling/lola
dumdum:x:1007::/dwelling/dumdum

With awk, you should use multiple delimiter. Within the following instance, two delimiters are specified, so the awk command accepts both a colon or a clean to separate fields. The primary two traces show the file, and the final two traces present the command and outcome.

$ cat file
Monday:1 Tuesday:2 Wednesday:3 Thursday:4 Friday:5
$ awk -F'[: ]' '{OFS=" ";print $1,$3,$4}' file
Monday Tuesday 2

Deciding on substrings

To pick out an arbitrary sequence or characters from a string, you should use an awk command just like the one beneath during which the $0 represents the complete phrase, 10 represents the primary character place to be grabbed and 5 is the size of the string to be displayed.

$ echo "Give attention to Peace" | awk '{print substr($0,10,5)}'
Peace

To do the identical type of factor with the minimize command, you’d use a command like this during which the 13th via 22nd characters are extracted from the phrase and displayed.

$ echo "Linux is a powerful OS" | minimize -c 13-22
spectacular

On this subsequent command, the minimize command shows the 7th-12th characters from the traces in a file. The head command merely limits the show to the primary 4 traces of output.

$ minimize -c 7-12 sayings | head -4
with 3
and ov
nd be
and be

Utilizing grep

You need to use the grep command to pick out a number of phrases from a file. On this instance, solely the chosen phrases are displayed, not the complete traces. It is because the -o (show solely the matched objects) possibility is getting used.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles