I want to pick up those rows whose 4th filed is not empty.
But the following RE did not work:
^\([^,]*,\)\{3\}[^,][^,]*,.*$
Then I tried to print the captured groups, the result confused me.
It seems that the repetition does not work.
Would anyone explain it, please.
Details(see line 4~6):
$ cat tmp
1AAA,BBB,CCC,DDD,EEE,FFF
2AAA,BBB,CCC,DDD,EEE,FFF
3AAA,BBB,,DDD,EEE,FFF
4AAA,BBB,CCC,,EEE,FFF
5AAA,BBB,CCC,,EEE,FFF
6AAA,BBB,CCC,,EEE,FFF
7AAA,BBB,CCC,DDD,EEE,FFF
8AAA,BBB,CCC,DDD,EEE,FFF
9xxxxxxx
$ sed -n "/^\(\([^,]*,\)\{3\}\)\([^,][^,]*\)\(,.*\)$/ {s//\1/;p;}" tmp
1AAA,BBB,CCC,
2AAA,BBB,CCC,
3AAA,BBB,,
4AAA,BBB,
5AAA,BBB,
6AAA,BBB,
7AAA,BBB,CCC,
8AAA,BBB,CCC,
$ uname
HP-UX
This awk will print all lines where 4th field is not empty.
awk -F, '$4' file
1AAA,BBB,CCC,DDD,EEE,FFF
2AAA,BBB,CCC,DDD,EEE,FFF
3AAA,BBB,,DDD,EEE,FFF
7AAA,BBB,CCC,DDD,EEE,FFF
8AAA,BBB,CCC,DDD,EEE,FFF
It may be simpler to work with awk in this case, since it simple to test fields.
Here it just test if $4 is not empty, and print the line.
Sure it is way simpler to do this with awk, as shown by the slim and perfectly working answer proposed by Jotne.
If you want to investigate what's wrong with your HP-UX sed thing, I would suggest you have a look at this conversation and try to pass your data not through a file but through the stdin of sed: cat tmp | sed -n ... or sed -n ... < tmp.
My very first attempt at troubleshooting your issue, though, would be to replace your double quotes by single quotes, as maybe with your double quotes your shell is trying to interpret $/ or *, I don't know what shell you are using...
You could try the below GNU sed command,
$ sed -nr '/^[A-Z0-9]+,[A-Z]*,[A-Z]*,[A-Z]+,[A-Z]*,[A-Z]*$/p' file
OR
$ sed -nr '/^.*,.*,.*,.+,.*,.*$/p' file
1AAA,BBB,CCC,DDD,EEE,FFF
2AAA,BBB,CCC,DDD,EEE,FFF
3AAA,BBB,,DDD,EEE,FFF
7AAA,BBB,CCC,DDD,EEE,FFF
8AAA,BBB,CCC,DDD,EEE,FFF
I have a file like:
35.26660,129.0373,'207636');
35.26667,129.0375,'207636');
35.55555,129.0377,'207636');
I want to delete last number between single quotes and the corresponding comma. I want the result to be like:
35.26660,129.0373);
35.26667,129.0375);
35.55555,129.0377);
I tried to use sed, but I made a mistake and I cannot see what is the problem.
If I use command:
sed "s/'//g"
Then I delete only the quotes
If I use command:
sed "s/[0-9]//g"
Then I delete only number between single quotes.
Update #1:
Sorry, in few lines I have a more data like:
'abc', '123' 35.26660,129.0373,'207636');
You can use this sed:
sed -i.bak "s/,'[^']*'//" file
It gives:
35.26660,129.0373);
35.26667,129.0375);
35.55555,129.0377);
I would do:
sed "s/,'[^']*')/)/"
Using awk
awk -F\' '{$2="";sub(/, +/,"")}1' file
35.26660,129.0373);
35.26667,129.0375);
35.55555,129.0377);
I have a csv file with fields like so:
"231444","344","some string","222"
I have been trying, without success, to remove the double quotes from around the integers in the csv. I have tried a bit of sed, and attempted to awk/gawk but I am really having trouble with this one. Expected output would be:
231444,344,"some string",222
There are no negative integers. Any help would be much appreciated, and thank you in advance.
Just for a reference. This things are can be done using Perl's one liner as well.
linux:
perl -i.bak -p -e 's/"(\d+)"/$1/g' input.txt
For reference, Windows(single quote doesn't work):
perl -i.bak -p -e "s/\"(\d+)\"/$1/g" input.txt
Your regex would be /"(\d+)"/g which should be replaced with \1.
Without knowing sed, I assume it'd be something like this (based on the Wikipedia example):
sed 's/"(\d+)"/\1/g' inputFileName > outputFileName
Regex 101 Demo
i am having trouble replacing the modified date in my script via sed.
I am getting the last modified date like this:
olddate=`grep -m1 "Built " script.sh | cut -c 22-29`
I get the current date with:
newdate=`date +%d/%m/%y`
Basically i want to replace old date with new date
sed -i "" "s/$olddate/$newdate/g" script.sh
But this doesn't work as the date contains slashes. I've looked around and i can't find the way to escape them properly. Any help would be appreciated.
You can use separators other than slashes, for instance ";"
sed -i "" "s;$olddate;$newdate;g" script.sh
Use , instead of / !
sed -i "" "s,$olddate,$newdate,g" script.sh
In fact you can use almost any char as separators.
use sed "s#$olddate#$newdate#g"
that should work