Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script substring till space

Tags:

linux

bash

I'd be most obliged to anyone who might be able to tell me how I can extract all characters up to the first space or tab character after having tested that what follows the space/tab is another specified substring? For example a file containing

php server-side
asp server-side
css client-side
html client-side
golang server-side

when read line-by-line could be used to generate the string

php asp golang

i.e. with the lines ending client-side dropped out altogether and the lines containing server-side truncated at the space.

like image 338
DroidOS Avatar asked Dec 04 '22 04:12

DroidOS


1 Answers

This command will do the trick,

grep "server-side" filename|cut -d ' ' -f1|tr '\n' ' '

Explanation as follows;

grep "server-side" filename

It will capture only lines matched with the string server-side.

cut -d ' ' -f1

cut commmand will cut first column of the table by delimiter space.

tr '\n' ' '

tr command will make all new line character replaced with a space.

Output will be exactly what OP mentioned in question(requirement):

php asp golang
like image 166
Skynet Avatar answered Dec 16 '22 09:12

Skynet