after some research here i ran across this use of SED :
sed 's/$/ /' filein > fileout
This worked as expected in some files, and added the spaces to the end of the line.
My problem is that in a different file like this :
AAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCC
and after running that same command to append some spaces (7) at the end of each line, it basicly adds a different line and puts the 7 spaces there, like :
AAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBB(CR)
spacesx7(cr)(lf)
BBBBBBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCC(CR)
spacesx7(cr)(lf)
Could someone help me? Thank you in advance
Try this:
dos2unix < filein | sed 's/$/ /' > fileout
Related
How can I add ",1" at the end of each of the lines using sed?
The file I am using is like this;
A
B
C
I tried this;
sed 's/$/,1/' alphabets.txt > alphabets1.txt
and got this;
A
,1
B
,1
C
,1
but what I want is this;
A,1
B,1
C,1
When I tried the best answer from "https://stackoverflow.com/questions/15978504/add-text-at-the-end-of-each-line", I got a blank page.
sed -n 's/$/,1/' alphabets.txt > alphabets2.txt
I found out that there's ^M at the end using cat -vt file. How can I delete it and change it to ",1"?
Mac sed is BSD sed. You may try this command on a file optionally ending with \r to remove \r and append 1 in the end:
sed -E $'s/\\\r?$/,1/'
A,1
B,1
C,1
Make sure you shell is bash while running this.
You may also use:
sed -E 's/'$'\r''?$/,1/' file
$'\r' is a BASH string to match \r and ? after this makes it optional.
Please note that this sed command will also run fine on gnu sed.
How can I delete lines that begin with # but not #!/bin/ksh?
Using sed -e '/^#/ d' sed.sh will delete every line including #!/bin/ksh.
Thanks #Daniel H
sed -e '/#!\/bin\/ksh/p' -e '/^#/d' sed.sh
this is the command that gave the result I was looking for:
Like this:
sed '/^#/{/^#!\/bin\/ksh/d}' sed.sh
For all lines that start with a #, if they don't start with #!/bin/ksh, delete them.
Since comments (and the shebang line) might have spaces in front, more precise would be
sed '/^[[:space:]]*#/{/^[[:space:]]*#![[:space:]]*\/bin\/ksh/d}' sed.sh
I have a file in which some lines start by a >
For these lines, and only these ones, I want to keep the first eleven characters.
How can I do that using sed ?
Or maybe something else is better ?
Thanks !
Muriel
Let's start with this test file:
$ cat file
line one with something or other
>1234567890abc
other line in file
To keep only the first 11 characters of lines starting with > while keeping all other lines:
$ sed -r '/^>/ s/(.{11}).*/\1/' file
line one with something or other
>1234567890
other line in file
To keep only the first eleven characters of lines starting with > and deleting all other lines:
$ sed -rn '/^>/ s/(.{11}).*/\1/p' file
>1234567890
The above was tested with GNU sed. For BSD sed, replace the -r option with -E.
Explanation:
/^>/ is a condition. It means that the command which follows only applies to lines that start with >
s/(.{11}).*/\1/ is a substitution command. It replaces the whole line with just the first eleven characters.
-r turns on extended regular expression format, eliminating the need for some escape characters.
-n turns off automatic printing. With -n in effect, lines are only printed if we explicitly ask them to be printed. In the second case above, that is done by adding a p after the substitute command.
Other forms:
$ sed -r 's/(>.{10}).*/\1/' file
line one with something or other
>1234567890
other line in file
And:
$ sed -rn 's/(>.{10}).*/\1/p' file
>1234567890
I'm trying to change the text in a file from this:
file.txt,c:\path\to\file
file.txt,c:\path with spaces\to\file
file.txt,c:\path\to\file with spaces
file.txt,c:\path\to\file with spaces
file.txt,c:\path\to\file with spaces
To this kind of output (one path to the file):
c:\path\to\file\file.txt
c:\path with spaces\to\file\file.txt
c:\path\to\file with spaces\file.txt
c:\path\to\file with spaces\file.txt
c:\path\to\file with spaces\file.txt
This ALMOST works but requires a ',' on the end of the line :
sed 's#\(.*\),\(.*\),\(.*\)#\2,\1,\3#g' file
Any help would be appreciated, I don't know sed all that well...
EDIT
This worked for me but I would still like to add a "\" in there:
sed 's#\(.*\),\(.*\)#\2,\1,\3#g' file
Escape the backslash, \\.
sed 's/^\(.*\),\(.*\)$/\2\\\1/g' file
--^^--
Also, you only need two capturing groups.
But since I'm more an awk guy.
awk -F, '{ print $2 "\\" $1 }' file
I am looking for I one liner hopefully, that can trim the first and last character of a line, on multiple lines e.g. test.txt
Before:
xyyyyyyyyyyyyyyyyyyyx
pyyyyyyyyyyyyyyyyyyyz
After:
yyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyy
$ cat /tmp/txt
xyyyyyyyyyyyyyyyyyyyx
pyyyyyyyyyyyyyyyyyyyz
$ sed 's/^.\(.*\).$/\1/' /tmp/txt
yyyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyyy
There is little trick :)
sed 's/^.(.*).$/\1/' file > file1 ; rm file ; echo file1 > file ; rm file1
sed -ne 's,^.\(.*\).$,\1,p'
This command will delete all lines that have less than two characters, since one cannot really strip the first and last character from them.