Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove first two words of a strings output

Tags:

linux

bash

sed

awk

cut

I want to remove the first two words that come up in my output string. this string is also within another string.

What I have:

for servers in `ls /data/field`    
do    
  string=`cat /data/field/$servers/time`

This sends this text:

00:00 down server

I would like to remove "00:00 down" so that it only displays "server".

I have tried using cut -d ' ' -f2- $string which ends up just removing directories that the command searches.

Any ideas?

like image 244
hisere Avatar asked Dec 15 '14 18:12

hisere


1 Answers

Please, do the things properly :

for servers in /data/field/*; do
    string=$(cut -d" "  -f3- /data/field/$servers/time)
    echo "$string"
done
  • backticks are deprecated in 2014 in favor of the form $( )
  • don't parse ls output, use glob instead like I do with data/field/*

Check http://mywiki.wooledge.org/BashFAQ for various subjects

like image 192
Gilles Quenot Avatar answered Sep 23 '22 10:09

Gilles Quenot