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
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%%:*}
0

Use cut instead :

cut -d ':' -f 1

Will select all before the first :.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.