I would like to replace the empty space between each and every field with comma delimiter.Could someone let me know how can I do this.I tried the below command but it doesn't work.thanks.
My command:
:%s//,/
53 51097 310780 1
56 260 1925 1
68 51282 278770 1
77 46903 281485 1
82 475 2600 1
84 433 3395 1
96 212 1545 1
163 373819 1006375 1
204 36917 117195 1
If you are talking about sed, this works:
sed -e "s/ /,/g" < a.txt
In vim, use same regex to replace:
s/ /,/g
Inside vim, you want to type when in normal (command) mode:
:%s/ /,/g
On the terminal prompt, you can use sed to perform this on a file:
sed -i 's/\ /,/g' input_file
Note: the -i option to sed means "in-place edit", as in that it will modify the input file.
I know it's not exactly what you're asking, but, for replacing a comma with a newline, this works great:
tr , '\n' < file
Try the following command and it should work out for you.
sed "s/\s/,/g" orignalFive.csv > editedFinal.csv
IF your data includes an arbitrary sequence of blank characters (tab, space), and you want to replace each sequence with one comma, use the following:
sed 's/[\t ]+/,/g' input_file
or
sed -r 's/[[:blank:]]+/,/g' input_file
If you want to replace sequence of space characters, which includes other characters such as carriage return and backspace, etc, then use the following:
sed -r 's/[[:space:]]+/,/g' input_file
If you want the output on terminal then,
$sed 's/ /,/g' filename.txt
But if you want to edit the file itself i.e. if you want to replace space with the comma in the file then,
$sed -i 's/ /,/g' filename.txt
I just confirmed that:
cat file.txt | sed "s/\s/,/g"
successfully replaces spaces with commas in Cygwin terminals (mintty 2.9.0). None of the other samples worked for me.
On Linux use below to test (it would replace the whitespaces with comma)
sed 's/\s/,/g' /tmp/test.txt | head
later you can take the output into the file using below command:
sed 's/\s/,/g' /tmp/test.txt > /tmp/test_final.txt
PS: test is the file which you want to use
Related
I got this text in file.txt:
Osmun.Prez#mail.com:c7lB2m6b#3.a.a:tt_webid_v2=6990226111024612869; tt_webid=6990226111024612869; tt_csrf_token=VD5Nb_TQFH4RKhoJeSe2nzLB; R6kq3TV7=AHkh4PB6AQAA3LIS90nWf2ss0Q7ZTCQjUat4axctvhQY68DdUEz92RwpmVSX|1|0|e9d6917c2fe555827dcf5ee916ba9778079ab2a9; ttwid=1%7CAFodeNF0iZM2fyy-ZeiZ6HTpZoG_MSx6SmXHgGVQ-V4%7C1627538859%7C59ca1e4a56f9f537b55e655a6dabff88e44eb48502b164ed6b4199f5a5263cb0; passport_csrf_token_default=6f7653c3ce946a6ce5444723fb0c509b; passport_csrf_token=6f7653c3ce946a6ce5444723fb0c509b; sid_guard=0483b7d37f4e4bd20ab3046e29724798%7C1627538893%7C5184000%7CMon%2C+27-Sep-2021+06%3A08%3A13+GMT; uid_tt=27b52febe6222486b9f6b6a90ef4ffeace5ea25c09d29a1583be5a1ecf760996; uid_tt_ss=27b52febe6222486b9f6b6a90ef4ffeace5ea25c09d29a1583be5a1ecf760996; sid_tt=0483b7d37f4e4bd20ab3046e29724798; sessionid=0483b7d37f4e4bd20ab3046e29724798; sessionid_ss=0483b7d37f4e4bd20ab3046e29724798; store-idc=maliva; store-country-code=us; odin_tt=294845c8f7711db177f7c549a9f44edb1555031b27a2a485df809cd92c4e544ac0772bf462df5b7a100f6e488c45303cd62df3b6b950f0842520cd887850137b035d990f29cc8b752765e594560c977f; cmpl_token=AgQQAPNSF-RMpbE89z5HYF0_-2PcrxjXf4fZYP5_ZA
How can I delete everything from the string inside ( first & only instance ) from :tt_ to _ZA in file.txt keeping only Osmun.Prez#mail.com:c7lB2m6b#3.a.a using bash linux?
Thank you
Something like:
sed -i "s/:tt_.*//" file.txt
if you want to edit the file in place. If not, remove the -i switch.
The sed command means: replace (s), in each line of file.txt, all the chars (.*) starting by the pattern :tt_ with an empty string (//).
Or the command:
sed -i "s/:tt_.*_ZA//" file.txt
which is more adherent to what you ask for, but returns the same output.
Use pattern substitution:
i=$(cat file.txt)
echo "${i/:tt*_ZA}"
Assuming the general requirement is to remove everything after the 2nd : ...
Sample data:
$ cat file.txt
Osmun.Prez#mail.com:c7lB2m6b#3.a.a:tt_webid_v ... to end of line
some.one#home.com:B52_m6b#9_az.more.stuff:delete from here ... to end of line
One sed idea:
$ sed -En 's/^([^:]*:[^:]*).*$/\1/p' file.txt
Osmun.Prez#mail.com:c7lB2m6b#3.a.a
some.one#home.com:B52_m6b#9_az.more.stuff
Using awk
awk 'BEGIN{FS=OFS=":"}{print $1,$2}'
Using : as the delimiter, it is easy to extract the columns before :tt
This deletes all chars from ":tt_" to the last "_ZA", inclusive, in file.txt
Mac_3.2.57$cat file.txt | sed 's/\(\)[:]tt.*_ZA\(.*\)/\1\2/'
Osmun.Prez#mail.com:c7lB2m6b#3.a.a
Mac_3.2.57$
Or if it is always the first 2 values which are separated by colon (as per you example)
cat file.txt | cut -f1,2 -dā:ā
I'm trying to replace the tab with 4 spaces, using sed, but it is not working.
Here is my code:
sed -i '{s/\t/ \{4\}/g}' filename
Any suggestion is appreciate.
In sed replacement is not supposed to be a regex, so use:
sed -i.bak $'s/\t/ /g' filename
On gnu-sed even this will work:
sed -i.bak 's/\t/ /g' filename
There is already an accepted answer but it does hardcoded basic tab expansion, while tabs have a variable width suitable for alignment, which is not taken into account in the previous answer. For example:
12\tabcd
1234\tabcd
should expand to the correctly aligned:
12 abcd
1234 abcd
but the given sed command will incorrectly expand to this misaligned output:
% printf "12\tabcd\n1234\tabcd\n" | sed 's/\t/ /g'
12 abcd
1234 abcd
The correct way to do it is to use the standard command expand, it's installed on all systems.
% printf "12\tabcd\n1234\tabcd\n" | expand
12 abcd
1234 abcd
If you want to use tabstops of size 4, pass -t 4.
I have several files with this line:
<2-10 digits> ; word
I want to replace all of the digits that come before that word with something else. How can I do that?
sed -i -e 's/.*word/something;word/g' <filename>
To loop over multiple files in a directory. I am assuming .txt file as the file extension:
for i in `\ls -1 *.txt`
do
sed -i -e 's/.*word/something;word/g' $i
done
Note: sed -i will modify the file interactively. So, test the command without -i option to check this is what you want and then go for it...
UPDATE: sed example:
s="999 abc 1234 ; word 567"
echo $s | sed 's/^\(.* \)[0-9][0-9]*\( ; word.*\)$/\1something\2/g'
OUTPUT:
999 abc something ; word 567
Can I increase some numbers in txt files with grep/sed?
I want to find all numbers in file and increase them for 5. Is that possible with grep and sed or I need to write app for that?
EDIT:
File has n lines which begin with number - number and than some text.
Like title for movie.
example line:
34 - 36 : Some text.
You can use perl as:
perl -i -pe 's/(\d+)/$1+5/eg' filename
See it
Probably awk. Change the record separator to whitespace (assuming this is what you want to do), then if a record matches the regex ^[0-9]*$ convert to number add 5 and print, otherwise print.
This is a pretty complete solution but "left as exercise" to code up.
I believe you should use awk Changing the Contents of a Field
>cat 1.txt
34 - 36 : Some text.
cat 1.txt | awk '{ $1=$1+5; $3=$3+5; print $0; }'
39 - 41 : Some text.
This might work for you (GNU sed & Bash):
sed 's/[0-9]\+/$((&+5))/g;s/.*/echo "&"/e' file
Edit: I'm using CYGWIN/GNU sed version 4.1.5 on windows Vista and I want a case insensitive search
I want to use sed to replace inline, the following:
c:\DEV\Suite\anything here --- blah 12 334 xxx zzzzz etc\Modules etc
Edit: anything here --- blah 12 334 xxx zzzzz etc means anything could appear here. Sorry for omitting that.
In a file with lines like
FileName="c:\DEV\Suite\anything here --- blah 12 334 xxx zzzzz etc\Modules\.... snipped ...."
with a value I supply, say :
Project X - Version 99.98
So the file ends up with:
FileName="c:\DEV\Suite\Project X - Version 99.98\Modules\.... snipped ...."
My attempt:
c:\temp>sed -r -b s/Dev\\Suite\\.*\\Modules/dev\\suite\\simple\\/g test.txt
However I get the following error:
sed: -e expression #1, char 42: unterminated `s' command
Thanks.
Edit:
I've already tried added quotes.
It's the '\\' before the '/'. Apparently you need 4 backslashes.
sed -r -b "s/Dev\\\\Suite\\\\.*\\\\Modules/dev\\\\suite\\\\simple\\\\/g" test.txt
I think the shell is interpreting the '\\' into a '\' before passing it to sed, and then sed is doing the same thing on what it gets.
Single quotes would work, so:
sed -r -b 's/Dev\\Suite\\.*\\Modules/dev\\suite\\simple\\/g' test.txt
If I use "\\\" where you have "\\", it works for me. With the double backslashes, the way it gets parsed evidently has a backslash escaping the terminating "/" of the substitution expression. (I still get the error if I replace ".*" with ".+".)
(Amusingly, I had to add more backslashes to get this to post properly -- SO ate a few of them!)
Got it: Replace the .* with .+
sed -r -b s/Dev\\Suite\\.+\\Modules/dev\\suite\\simple\\/g test.txt
I don't know what version of sed your using. I'm not familiar with the -b option.
First, I'd suggest using the i regex flag, to make it case insensitive. Your example of DEV won't match your regex of Dev.
I suspect the problem your running into is how your version of sed interprets backslash characters.
I'd suggest using the sed bundled with Cygwin. With single quotes, it seems to work for me.
echo 'c:\DEV\Suite\anything here --- blah 12 334 xxx zzzzz etc\Modules\' | sed -r 's/Dev\\Suite\\.*\\Modules/dev\\suite\\simple\\/gi'
c:\dev\suite\simple\\
well...
sed -e s/"anything here --- blah 12 334 xxx zzzzz etc"/"Project X - Version 99.98"/g test.txt
worked fine
(The compliant about the unterminated 's' was because of the unescaped '/')
Funny I was having the same issue in one directory but the same command worked in other directories on the same machine. This is the command I was working with
export version=grep "version.*SNAPSHOT.*version" pom.xml |sed -e 's|<version>||g'|sed -e 's|</version>||g'|sed -e "s|\t* *||g"; cat sonar-project.properties.template |sed -e "s/BUILDVERSION/$version/g">sonar-project.properties
when I changed the * to + it worked.
Thanks :-)
Will rename:
TV Show - 376 [720p].mkv
TV Show - 377 [720p].mkv
to
376.mkv
377.mkv
works under cygwin.
#!/bin/bash
for i in *; do
mv "$i" "`echo $i | sed -r -b 's/^.*[ ]([0-9]*)[ ].*$/\1.mkv/'`";
done