how to use sed to delete the path? - sed

The contents of file:
/usr/local/foo/test
/usr/local/foo/testlog
how to use sed to delete one of them?
FSNAME=/usr/local/foo/test OR /usr/local/foo/testlog
sed -i "s#${FSNAME}#EXCLUSIVE#;/\<EXCLUSIVE\>/d" /tmp/file
this does not work, please help me?
thks

You can try the following code:
FSNAME=$(echo "/usr/local/foo/test /usr/local/foo/testlog" | xargs shuf -n1 -e)
sed -i.bak "s:$FSNAME:EXCLUSIVE:;/EXCLUSIVE/d" file
the shuf command will allow you to choose randomly between the 2 paths,
then the sed command will delete the line containing only this path.
test (without editing the file)

Related

manipulation of text by sed command

I a file containing the genome ids following NZ_FLAT01000030.1_173 I need to manipulate those ids like this one: NZ_FLAT01000030.1
I tried some but didn't give me the exact thing.
sed 's/_/\t/' output : NZ FLAT01000030.1_173
sed -r 's/_//' output: NZFLAT01000030.1_173
sed -r 's/_//g' output: NZFLAT01000030.1173
How can I do that by using sed command?
Are you trying to remove the undesrscore and the digits following it?
echo 'NZ_FLAT01000030.1_173' | sed -E 's/_[0-9]+//g'
NZ_FLAT01000030.1
$ echo 'NZ_FLAT01000030.1_173' | sed 's/_[^_]*$//'
NZ_FLAT01000030.1

Better way to fix mocha lcov output using sed

Due to the know prob of mocha-lcov-mocha breaking file paths, I need to fix the current output paths that looks like this:
SF:Vis/test-Guid.coffee
SF:Vis/Guid.coffee
SF:Vis/test-Vis-Edge.coffee
SF:Vis/Vis-Edge.coffee
into
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
I'm not very good with sed, but I got it to work using:
mocha -R mocha-lcov-reporter _coverage/test --recursive | sed 's,SF:,SF:src/,' | sed s',SF.*test.*,SF:test//&,' | sed s',/SF:,,' | sed s',test/src,test,' | ./node_modules/coveralls/bin/coveralls.js
which is basically doing 4 sed commands in sequence
sed 's,SF:,SF:src/,'
sed s',SF.*test.*,SF:test//&,'
sed s',/SF:,,'
sed s',test/src,test,'
my question is if there is a way to do with this one sed command, or use another osx/linux command line tool
Initially put "src/" after every ":" and then if "test" is found on the line replace "src" with "test":
$ sed 's,:,:src/,;/test/s,src,test,' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
You could put all the sed commands in a file, one line per command, and just use "sed -e script". But if you just want it on a single command-line, separate with semicolons. This works for me:
sed 's,SF:,SF:src/,;s,SF.*test.*,SF:test//&,;s,SF:,,;s,test/src/,test,'
sed command
sed '\#test#!{s#SF:Vis/#SF:src/Vis/#g};\#SF:Vis/test#{s#SF:Vis/test#SF:test/Vis/test#g};' my_file
Here is an awk version:
awk -F: '/SF/ {$0=$1FS (/test/?"test/":"src/")$2}1' file
SF:test/Vis/test-Guid.coffee
SF:src/Vis/Guid.coffee
SF:test/Vis/test-Vis-Edge.coffee
SF:src/Vis/Vis-Edge.coffee
How it works:
awk -F: ' # Set field separator to ":"
/SF/{ # Does line start with "SF"?
$0=$1FS (/test/?"test/":"src/")$2 # Recreat String by adding "test" if line contains "test", else "src"
}
1 # Print all lines
' file # read the file

how to replace softtabs with hardtabs in sed

I am trying to replace softtabs with hardtabs in sed. I have tried the following but to no avail:
sed -i 's/ /\t/g' path/to/file
What am I doing wrong?
It appears what I was looking for was the unexpand command.
unexpand -a -t4 file > newfile
If you have a file like this where all space is spaces and not tabs:
cat file
test more data
here are more
You can use
sed 's/ */\t/g'
or
sed -r 's/ +/\t/g'
and get
test more data
here are more
Where it now have changed multiple spaces to tab

how to remove trailing line from every file with our renaming the fuel

Hi I use the following code to remove the trailing lines from a file. But is there anyway I can run this on 2000 files inside a folder with out renaming them ? thanx in advance
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
sed -i "" -e :a -e '/^\n*$/{$d;N;};/\n$/ba' YourFile
if it is in same folder with a pattern to select id like *.txt, replace YourFile by the shell pattern, if other selection pass each file name via a pre selection like find or a while read from a stream/file input
You can use find to list the files that you need, and then run sed on each on them. This version uses the -i option to modify the files in place:
find . -name "*.txt" | xargs -I % sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' %

restricting sed command

I have a variables that needs to be modified so that minutes %M is added. I know the sed command and it is working as expected.
# cat mysed.txt
myfirstfile="comany$mydb`date +'%d-%b-%Y-%H'`.sql"
# sed -i 's/\%H/\%H-\%M/' mysed.txt
# cat mysed.txt
myfirstfile="company$mydb`date +'%d-%b-%Y-%H-%M'`.sql"
But if I run the same sed command again, it will add %M again as follows.
# sed -i 's/\%H/\%H-\%M/' mysed.txt
# cat mysed.txt
myfirstfile="company$mydb`date +'%d-%b-%Y-%H-%M-%M'`.sql"
I need a sed command that should add the minutes %M only once even if sed command is executed twice (by mistake)
# sed -i "s/%H'/%H-%M'/" mysed.txt
This should work. This way it will only do the replacement if there is a quote mark next to the %H.
Test if it is present before substituting:
sed '/%H-%M/! s/%H/%H-%M/' mysed.txt
Btw. % does not need to be escaped.
I believe below command will work fine.Please replace test.dat file with your file name.
cat test.dat|sed "s/\%H\'/\%H\-\%M\'/" > test.dat
Without cat command
sed -i "s/\%H\'/\%H\-\%M\'/" test.dat > test.dat
Cheers!