0

I have a text line that looks like this (there is a space after "poseidon"):

/usr/share/geoserver/data_dir/workspaces/poseidon public/odense_kommune/datastore.xml:17:host">a-postgis1.gues.com</entry>

and I use this to delete everything after the first ":"

filename=$(sed 's/\:.*$//' <<< $myHost)

The result is :

/usr/share/geoserver/data_dir/workspaces/poseidon
public/datastore.xml

and it should have been :

/usr/share/geoserver/data_dir/workspaces/poseidon public/datastore.xml

it replaces the space with with a new line

I have tried with several SED options but can't figure out what is wrong.

I use the above like this :

filename=$(sed 's/\:.*$//' <<< $myHost)
echo Filnavn : $filename >> filn.txt
neurino
  • 1,809
  • 3
  • 19
  • 25
Julian
  • 1
  • 1
  • Hello Julian, you should really format your code: https://unix.stackexchange.com/editing-help I edited your question, it should be available after peer review. Cheers – neurino Oct 23 '18 at 14:13
  • Please add how you assign `$myHost`? – pLumo Oct 23 '18 at 14:28
  • 1
    Remember to quote your variable expansions to prevent split+glob – Stéphane Chazelas Oct 23 '18 at 16:13
  • Are you *sure* that is a space in the first place? Try this: `echo "$myHost" | od -c` – glenn jackman Oct 23 '18 at 16:17
  • You might also want to read [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) – glenn jackman Oct 23 '18 at 16:18

3 Answers3

2

Your sed command works for me.

But no need for sed, you can use shell parameter expansion to remove everything from the first : to the end:

filename=${myHost%%:*}
pLumo
  • 22,231
  • 2
  • 41
  • 66
0

Use cut instead :

cut -d ':' -f 1

Will select all before the first :.

MUY Belgium
  • 1,234
  • 2
  • 14
  • 31
0

Your sed command works for me.

$ cat test 
/usr/share/geoserver/data_dir/workspaces/poseidon public/odense_kommune     /datastore.xml:17:host">a-postgis1.gues.com</entry>

SED

$ cat test  | sed 's/\:.*//'
/usr/share/geoserver/data_dir/workspaces/poseidon public/odense_kommune/datastore.xml

If you can't use sed to solve this problem, you can try cut or awk.

CUT

$ cat test  | cut -d ':' -f1
/usr/share/geoserver/data_dir/workspaces/poseidon public/odense_kommune/datastore.xml

AWK

$ cat test  | awk -F ':' '{printf $1}'
/usr/share/geoserver/data_dir/workspaces/poseidon public/odense_kommune/datastore.xml
zsnmwy
  • 1