Sed replace spaces and a character before end of line - text

I have this line:
\\Server1\A Share & Test & Check M
I want this output:
\\Server1\A Share & Test & Check
It should end with a character and not spaces (\\Server1\A Share & Test & Check)
I tried this:
sed -i "s/[ *\t[a-z]]*$//I" shares.txt
It removes the last letter but not the spaces.

The regex you are after is \s*[a-z]*$
sed -i "s/\s*[a-z]*$//I" shares.txt
\s is for any white space character

try this
echo "\\Server1\A Share & Test & Check M" | sed 's/[\tA-Za-z]*$//g'
output:
echo "\\Server1\A Share & Test & Check

Related

Shell script to replace string having space with another string

I'm trying to replace following string to multi lines as following
setsid /usr/local/bin/Naming_Service ${OPTIONS} &
replacing with
setsid /usr/local/bin/Naming_Service ${OPTIONS_13016} &
setsid /usr/local/bin/Naming_Service ${OPTIONS_13018} &
I tried with this command
sed '0,/setsid \/usr\/var\/run\/Naming_Serivce ${OPTIONS}/s//setsid \/usr\/var\/run\/Naming_Serivce ${OPTIONS_13016}\n\setsid \/usr\/var\/run\/Naming_Serivce ${OPTIONS_13018}\n /' script > new_script
can you please help to resolve
sed 's/^\(.*\)\(${OPTIONS}\)\(.*\)$/\1${OPTIONS_13016}\3\n\1${OPTIONS_13018}\3/' < script > new_script
(...) - create groups
\1 \3 - using these groups
\n - newline
.* - any character
For Your requirement use below syntax
Syntax:
sed -e "s/setsid \/usr\/local\/bin\/Naming_Service \${OPTIONS}/setsid \/usr\/local\/bin\/Naming_Service \${OPTIONS_13016} \&\nsetsid \/usr\/local\/bin\/Naming_Service \${OPTIONS_13018}/g" script > new_script

How to replace the a specific character in sed command which have predefine meaning?

I have this text
" File: 'space folder' "
I want to replace this with only this
" space folder "
using sed or awk?
But when i try to do with it using sed it's not taking the command!
Does anyone have solution for this.
If I get your intent correctly, you need all text between single quotes; you can use this:
$ sed -r "s/^.*'([^']*)'.*$/\"\1\"/g" <<< "\" File: 'space folder' \""
"space folder"
$
Edit1: explanation
command <<< string => <<< indicates here string that is you pass a string to the command.
Our final string is this:
$ echo -e "\" File: 'space folder' \""
" File: 'space folder' "
$
since our string contains single quotes we use double quotes for sed command:
-r switch enables extended regular expression
"s/^.*'([^']*)'.*$/\"\1\"/g"
the above command substitutes the whole line with text present between single quotes.
Regular expression breakdown:
^ matches start of line
.* matches 0 or more characters
' matches a literal single quote
([^']*) matches 0 or more characters that are not single quote
and remembers it as a captured group with backreference number \1
' matches literal single quote
.* matches 0 or more chars
$ matches end of line

Bash Script- Bash Script - Editing Lines on Text From File

I'm using a bash script to read in data from a text file.
Data:
04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q
[ARMADA MUSIC]
07:46 Fabio XB & Liuck feat. Christina Novelli - Back To You (Wach Remix)http://goo.gl /yGxqRX
[DIGITAL SOCIETY RECORDINGS]
code:
#!/bin/bash
file="/home/nexusfactor/Desktop/inputData(linux).txt"
while IFS= read -r line
do
# display $line or do somthing with $line
echo "$line"
done <"$file"
I would like to remove the white space between the two songs, then remove the time at the beginning of the song and the hyperlink/studio name from the end of the file. So my output would be:
Alex M.O.R.P.H. & Natalie Gioia - My Heaven
Fabio XB & Liuck feat. Christina Novelli
echo " 04:31 Alex M.O.R.P.H. & Natalie Gioia - My Heaven http://goo.gl/rMOa2q [ARMADA MUSIC]
07:46 Fabio XB & Liuck feat. Christina Novelli - Back To You (Wach Remix) http://goo.gl/yGxqRX [DIGITAL SOCIETY RECORDINGS]" \
| sed '/^[ \t]*$/d;s/^[0-9][0-9]:[0-9][0-9] //;s/http:.*//'
output
Alex M.O.R.P.H. & Natalie Gioia - My Heaven
Fabio XB & Liuck feat. Christina Novelli - Back To You (Wach Remix)
# ---------------------------------------^----- ????
Not quite what your example output shows, but matches your written requirement
remove the time at the beginning of the song and the hyperlink/studio name from the end ...
Rather than read each line in a while loop, use seds built in ability to read each line of a file and process it. you can do
sed '/^[ \t]*$/d;s/^[0-9][0-9]:[0-9][0-9] //;s/http:.*//' file > newFile && /bin/mv newFile file
OR if you're using a modern linux environment (and others), use the -i option to overwrite the existing file :
sed -i '/^[ \t]*$/d;s/^[0-9][0-9]:[0-9][0-9] //;s/http:.*//' file
IHTH
#!/bin/bash
file="/home/nexusfactor/Desktop/inputData(linux).txt"
while read -r date line
do
[[ $date == "" ]] && continue # empty line -> next loop
[[ $date =~ ^\[ ]] && continue # line starts with "[" -> next loop
line="${line%(*}" # remove "(" and everything to the right of it
line="${line%http*}" # remove "http" and everything to the right of it
echo "$line"
done <"$file"
Output:
Alex M.O.R.P.H. & Natalie Gioia - My Heaven
Fabio XB & Liuck feat. Christina Novelli - Back To You

Replace 2nd occurance of a special character after nth occurance of a delimiter from string,in unix/linux

Here My question is,
Replace 2nd or all occurance of a special character after nth occurance of a delimiter from string,in unix/linux
or
Replace "Text Qualifier" character from data field in unix.
I have below string where '"'(Double Quote) should get replaced with space.
String:
"123"~"23"~"abc"~24.50~"descr :- nut size 12" & bolt size 12"1/2, Quantity=20"~"2013-03-13"
From above string, i want below output:
"123"~"23"~"abc"~24.50~"descr :- nut size 12 & bolt size 12 1/2, Quantity=20"~"2013-03-13"
I have replaced " double quote character with space character.
"descr :- nut size 12" & bolt size 12"1/2, Quantity=20"
&
"descr :- nut size 12 & bolt size 12 1/2, Quantity=20"
I want to identify such rows from file & would like to replace such text qualifier character from data in Unix/Linux.
Request you to provide your inputs, & thanking you in advance.
I would use plain read to get the fields and then modify the wished ones using sed or shell variable substitution mechanisms:
echo '"123"~"23"~"abc"~24.50~"descr :- nut size 12" & bolt size 12"1/2, Quantity=20"~"2013-03-13"' | {
IFS='~' read a b c d e f
printf "%s~%s~%s~%s~%s~%s" "$a" "$b" "$c" "$d" "$(sed 's/"/ /g' <<<$e)" "$f"
# or:
printf "%s~%s~%s~%s~%s~%s" "$a" "$b" "$c" "$d" "${e//\"/ }" "$f"
}
That IFS ("Internal Field Separator") is an internal variable telling the shell how to separate fields, e.g. when using read. In our case using this tells the shell to use ~ as separator. Prepending the assignment directly to the read command makes that assignment only for the duration of the read command.
I'm going to assume you work in the bash shell.
awk(awk) can help you split your input string at the right positions, using its "-F" option:
echo xyzabcdef | awk -Fb '{print $1}'
gives you "xyza", the the first string before the separator.
Then, the tr(1) utility can help you replace characters:
tr '"' ' '
will replace '"' with ' '. I hope this helps to get you in the right direction.

How to use grep to extract a line with inconsistent number of space character

Like I have a text file with,
+
Code here
+
Code here +
Code+ here
+
Code here
And I want to grep this file and only show the line with a + character only? What regex should I construct?
Please advise
Many thanks
If I understand correctly, you want the line with only a +, no whitespace. To do that,
use ^ and $ to match the beginning and end of the line, respectively.
grep '^+$' filename
If you want a line with nothing but the + character, use:
grep '^+$' inputFile
This uses the start and end markers to ensure it only has that one character.
However, if you want lines with only a + character, possibly surrounded by spaces (as seems to be indicated by your title), you would use something like:
grep '^ *+ *$' inputFile
The sections are:
"^", start of line marker.
" *", zero or more spaces.
"+", your plus sign.
" *", zero or more spaces.
"$", end of line marker.
The following transcript shows this in action:
pax> echo ' +
Code here
+
Code here +
Code+ here
+
Code here' | grep '^ *+ *$' | sed -e 's/^/OUTPUT:>/' -e 's/$/</'
OUTPUT:> +<
OUTPUT:> +<
OUTPUT:> + <
The input data has been slightly modified, and a sed filter has been added, to show how it handles spacing on either side.
And if you want general white space rather than just spaces, you can change the space terms from the current " *" into "\s*" for example.

Resources