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
Related
I have a file with content like so - #1: 00001109
Each line is of the same format. I want the final output to be #1: 00 00 11 09.
I used command in sed to introduce a space every 2 characters - sed 's/.\{2\}/& /g'. But that will give me spaces in the part before the colon too which I want to avoid. Can anyone advise how to proceed?
Could you please try following, written and tested with shown samples.
awk '{gsub(/../,"& ",$2);sub(/ +$/,"")} 1' Input_file
Explanation: First globally substituting each 2 digits pair with same value by appending space to it where gsub is globally substitution to perform it globally). Once this is done, using single sub to substitute last coming space with NULL to avoid spaces at last of lines.
With sed:
sed -E 's/[0-9]{2}/& /g;s/ +$//' Input_file
Explanation: Globally substituting each pair of digits with its same value and appending spaces to it. Then substituting space coming last space of line(added by previous substitution) with NULL.
This might work for you (GNU sed):
sed 's/[0-9][0-9]\B/& /g' file
After a pair of digits within a word, insert a space.
If perl happens to be your option, how about:
perl -pe '1 while s/(\d+)(\d\d)/$1 $2/g' file
you can use pure bash:
for line in "$(<your_file.txt)"; do
first=`echo $line | cut -d' ' -f1`" "
last=`echo $line | cut -d' ' -f2`
for char in `seq 0 2 ${#last}`; do
first+=${last:$char:2}" "
done;
done;
Given the contents of test.txt as follows:
Hello 10 love 20 haha 30
Hello Hello 11 love love 21 haha 31
41 Hello Hello 42 love love 43 haha 44
I want some kind of grep expression so that after saying:
$ cat test.txt | grep ???
I get this output:
20
21
42
How to implement this function?
Seems like you're trying to get the second number..
grep -oP '^\D*\d+\D*\K\d+' file
or
Use sed.
sed 's/^[^[:digit:]]*[[:digit:]]\+[^[:digit:]]*\([[:digit:]]\+\).*/\1/' file
DEMO
An alternative you might like to consider, using awk:
awk -F'[^[:digit:]]+' '{ print /^[[:digit:]]/ ? $2 : $3 }' file
This sets the field separator to one or more non-digit characters, which means that the field you're interested in is either the second or the third field, depending on whether the line starts with a digit or not.
For brevity you may prefer to use the range [0-9] instead of [[:digit:]]:
awk -F'[^0-9]+' '{ print /^[0-9]/ ? $2 : $3 }' file
Or you could use perl to capture the part of the line you're interested in:
perl -lne 'print $1 if /\d\D+(\d+)/' file
\d matches digits and \D matches non-digits, so this captures the second set of digits found on the line. In the case where a second set of digits aren't found, nothing will be printed (this differs to the behaviour of the awk script).
I'm trying to replace a number in a file using sed. This number can be found using \b<NUMBER>\b. However, there are comments in the file I'm parsing that sometimes have the same number and I would like to leave them unchanged.
All the lines that need to be replaced are similar to:
some_text <1 4 35 314 359>
And the complete file could be something like:
# This is not to be replaced: 314
some_text <1 4 35 314 359>
So, if I wanted to replace 314, how could I do it with sed?
I can find it with the following grep:
grep -P "^[^#].*some_text <[ 0-9]*>" "<FILE>" | grep -e "\b314\b")
But I can't seem to figure out a way to do it with sed. The old line I had would replace all the entries for that number:
sed -i "s/\b *314\b//" <FILE>
Any clarifications or help would be most welcome!
Thank you for your help!
/G
You can use sed like this:
sed '/some_text/s/\b314\b/789/' file
# This is not to be replaced: 314
some_text <1 4 35 789 359>
You could use awk instead, skipping any lines that are comments:
awk '!/^#/{sub(/\y314\y/,789)}1' file
As you've used word boundaries in your example, I'm assuming that you have GNU awk installed and I've used \y, which is a word boundary.
Does anyone know any unix commands/perl script that would replace a specific occurence
my file is hello.txt
number 555
number 555
number 555
now i want to replace the second occurence with number 666.
i have been trying this command
perl -n -i -e "s/number\\s+555/number 666/g" hello.txt'
which is changing all the occurences.
one liners will be really helpful.
$. holds line number for current file handle and can be used for given input file like,
perl -i -pe 's/number\s+555/number 666/ if $. == 2' hello.txt
or if number part can be dropped out,
perl -i -pe 's/555/666/ if $. == 2' hello.txt
I read content of file hello.txt to array then joined to get $str. Here it will replace 2nd occurrence with i initialized to 0. Try this search and replace in one liner.
$str =~ s/(number\s+555)/ ++$i==2 ? "number 666": $1/gse;
Does anyone know any unix commands: I believe awk is suitable for this task (http://www.grymoire.com/Unix/Awk.html).
awk '{if (NR == 2) {gsub("555", "666", $0);} print $0; } ' hello.txt
Try using sed
sed -r -i.bak ':a;N;$!ba;s/(number\s+)555/\1666/2' file
Output:
number 555
number 666
number 555
Reference SO question
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