How to remove part of text after certain sign in bash - sed

I have .txt file which has following data:
user-5
user-10
user-12
user-23(some text)
user-11#dsa.dsd
user-23-sometext
I want to leave only user-NUMBER. So I have to remove text after #, ) and second -.
I'm trying to use sed command, already succed with # and ). How can I remove text after second -?
My code: sed 's/[)|#].*//g'

sed 's|\(user-[0-9]*\).*|\1|'
This way you don't need to include every possible character that would terminate a user-NUMBER match.

Related

How can I search and replace for two characters with a string in between and only replace the latter character using sed?

Take the following string, for example:
some random text*
- sed is actually not that easy.*
other text
How can I search for lines containing both - and *, then replace the asterisk in that line with a string?
(For Example) Using the string "test" to replace the asterik in matched lines, the output would look this:
some random text*
- sed is actually not that easy.test
other text
I tried
sed -i '' 's/\- .*\*/\n\n:::\n/g';
But the problem with that is that it replaces the whole line, instead of just the asterisk.
If you want to match and keep the hyphen at the start of the line, and replace the asterix at the end of the line, you can use a capture group \(...\) for what you want to keep and use that group in the replacement with \1
sed -i 's/^\(- .*\)\*$/\1test/' file
The contents of file will be:
some random text*
- sed is actually not that easy.test
other text
If the characters are not directly at the start or end of the string, you can remove the anchors ^ and $

Why won't the tab be inserted on the first added line?

I am trying to add multiple lines to a file, all with a leading a tab. The lines should be inserted on the first line after matching a string.
Assume a file with only one line, called "my-file.txt" as follows:
foo
I have tried the following sed command:
sed "/^foo\$/a \tinsert1\n\tinsert2" my-file.txt
This produces the following output:
foo
tinsert1
insert2
Notice how the the tab that should be on the first (inserted) line is omitted. Instead it prints an extra leading 't'.
Why? And how can I change my command to print the tab on the first line, as expected?
With GNU sed:
sed '/^foo$/a \\tinsert1\n\tinsert2' file
<---- single quotes! --->
Produces:
foo
insert1
insert2
From the manual:
a \
text Append text, which has each embedded newline preceded by a backslash.
Since the text to be append itself has to to be preceded by a backslash, it needs to be \\t at the beginning.
PS: If you need to use double quotes around the sed command because you want to inject shell variables, you need to escape the \ which precedes the text to be appended:
ins1="foo"
ins2="bar"
sed "/^foo\$/a \\\t${ins1}\n\t${ins2}" file
sed is for doing s/old/new on individual strings, that is all. Just use awk:
$ awk '{print} $0=="foo"{print "\tinsert1\n\tinsert2"}' file
foo
insert1
insert2
The above will work using any awk in any shell on every UNIX box and is trivial to modify to do anything else you might want to do in future.

sed match first word replace full line

I know this should be straight forward but I'm stuck, sorry.
I have two files both contain the same parameters but with different values. I'm trying to read one file line at a time, get the parameter name, use this to match in the second file and replace the whole line with that from file 1.
e.g. rw_2.core.fvbCore.Param.isEnable 1 (FVB_Params)
becomes
rw_2.core.fvbCore.Param.isEnable true (FVB_Boolean)
The lines are not always the same length but I always want to replace the whole line.
The code I have is as follows but it doesn't make the substitutions and I can't work out why not.
while read line; do
ParamName=`awk '{print $1}'`
sed -i 's/$ParamName.*/$line/g' FVB_Params.txt
done < FVB_Boolean.txt
You need your sed command within double quotes if you want those variables to be replaced with their values. You have single quotes, so sed is actually looking for strings with dollar signs to replace with the string '$line', not whatever your shell has in the $line variable.
In short, sed's not seeing the values you want. Switch to double quotes.

Use sed to replace ony one occurrence in a certain block

I want to replace ignore_broadcast_ssid=1 with ignore_broadcast_ssid=0
inside the file /var/run/hostapd-phy0.conf.
This would be my first guess:
sed 's/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/g' /var/run/hostapd-phy0.conf
But this replaces this option globally, How can I only replace this in one of the sections, starting with bss=wlan0-2 inside the file?
...
bss=wlan0-2
ctrl_interface=/var/run/hostapd
ap_isolate=1
disassoc_low_ack=1
preamble=1
wmm_enabled=1
ignore_broadcast_ssid=0
uapsd_advertisement_enabled=1
auth_algs=1
wpa=0
ssid=temp_wifi
bridge=br-client
bssid=a0:f3:c1:d8:b7:7c
interface=client0
ctrl_interface=/var/run/hostapd
ap_isolate=1
disassoc_low_ack=1
preamble=1
wmm_enabled=1
...
You can first find out the line number of the first text as the starting search index:
grep -n "bss=wlan0-2"
Let assume it is at Line 10. Then apply your sed command at follow:
sed '10s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/' /var/run/hostapd-phy0.conf
Make sure you don't have the keyword g at the end of the command as it indicates to replace the matching pattern globally.
sed '/bss=wlan0-2/,/ignore_broadcast_ssid/{s/ignore_broadcast_ssid=1/ignore_broadcast_ssid=0/}' file

Printing all words that start with "#" using sed in BASH

I have a file with a lot of text, but I want to print only words that contain "#" at the beginning. Ex:
My name is #Laura and I live in #London. Name=#Laura. City=#London
How can I print all words that start with #?.I did this the following and it worked, but I want to do it using sed. I tried several patters, but I cannot make it print anything.
grep -o -E "#\w+" file.txt
Thanks
Use this sed command:
sed 's/[^#]*\(#[^ .]*\)/\1\n/g' file.txt
Explanation: we invoke the substitution command of sed. This has following structure: sed 's/regex/replace/options'. We will search for a regex and replace it using the g option. g makes sure the match is made multiple times per line.
We look for a series of non at chars followed by an # and a number of non-spaces #[^ ]*. We put this last part in a group \(\) and sub it during the replacement \1.
Note that we add a newline at the end of each match, you can also get the output on a single line by omitting the \n.