remove comma from last line - sed

How do I remove the comma from the last line of the file?
Here is the file:
# cat ox_data_archive_r_20120727.json
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"},
The following will remove the comma from all 3 lines.
# sed 's/,$/\ /' ox_data_archive_r_20120727.json
{"name": "secondary_ua","type":"STRING"}
{"name": "request_ip","type":"STRING"}
{"name": "cb","type":"STRING"}
I need to remove the last comma only. So the output should look something like this...
# cat newfile.json
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"}

$ cat input.txt
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"},
$ sed '$s/,$//' < input.txt
{"name": "secondary_ua","type":"STRING"},
{"name": "request_ip","type":"STRING"},
{"name": "cb","type":"STRING"}
From the documentation of GNU sed:
$: This address matches the last line of the last file of input, or the last line of each file when the -i or -s options are specified.

This should work:
sed '$ s/,$//g' input_file
The first $ selects the last line.
You could add -i and sed will apply the changes to your input_file.

The awk answer is certainly more verbose than the sed:
awk 'NR>1 {print prev} {prev=$0} END {sub(/,$/,"", prev); print prev}' file

Related

Merging Lines using sed

I have text file that consists of 45999 lines. Each line has a word (unigram). I want to create two-sequential words (bigrams). For example:
apple
pie
red
vine
I want 'apple pie', 'pie red', 'red vine'. I tried with sed 'N;s/\n/ /' but it creates just 'apple pie' and 'red vine'. How can I solve this problem? Thank you..
Could you please try following if you are ok with awk.
awk -v RS="" '
BEGIN{
OFS=","
s1="\047"
}
{
for(i=2;i<=NF;i++){
print s1 $(i-1) s1, s1 $i s1
}
}' Input_file
Output will be as follows.
'apple','pie'
'pie','red'
'red','vine'
2nd solution: since output of OP is not clear so adding this one too.
awk -v RS="" '
BEGIN{
OFS=","
s1="\047"
}
{
for(i=2;i<=NF;i++){
val=(val?val OFS:"")s1 $(i-1) s1 OFS s1 $i s1
}
}
END{
print val
}' Input_file
Output will be as follows.
'apple','pie','pie','red','red','vine'
This might work for you (GNU sed):
sed -nE 'N;s/\n(.*)/ \1&/;P;D' file
Append the next line to the current line, then replace the newline by a space and append the second line again. Print/delete the first line and repeat.
N.B. This does not print the last line as it is not a pair, if the last line is needed use:
sed -E 'N;s/\n(.*)/ \1&/;P;D' file
If the output is to be printed as a single line with each pair surrounded by single quotes and separated by a comma, use:
sed -E ':a;$!N;s/(\S+)\n(.*)/'\''\1 \2'\'', \2/;ta;s/ (\S+)$/ '\''\1'\''/' file
Or:
sed -E ':a;$!N;s/(\S+)\n(.*)/'\''\1 \2'\'', \2/;ta;s/, \S+$/' file

Use sed with special character

I try to use sed with special character:
sed -i -e "1i$jtempo" $file
$file is my file.
$jtempo is my variable with special character like: " or [ or (
But, when I run this script, I have this error:
sed: -e expression #1, char 17: unknown command: `"'
My file:
"desc_test":[
"id",
"name",
],
In my script bash:
jtempo=`cat myfile`
An idea ?
Thanks you !
$ cat f1
"desc_test":[
"id",
"name",
],
$ cat ip.txt
1
2
3
I would suggest to avoid i command and use r command which will be robust regardless of file content
$ # to insert before first line
$ cat f1 ip.txt
"desc_test":[
"id",
"name",
],
1
2
3
$ # to insert any other line number, use line_num-1
$ # for example, to insert before 2nd line, use 1r
$ # r command will read entire contents from a file
$ sed '1r f1' ip.txt
1
"desc_test":[
"id",
"name",
],
2
3

Sed help requred

I like to find some thing with sed in file that have many occurrence
File As below
"xyz": "somename_dsa", some other text, "xyz": "zcbr53", some other text, "xyz": "zms53",
Item needed
I need text after "xyz" :
Using gnu awk
awk -v RS='"xyz"' 'NR>1 {print $2}' file
"somename_dsa",
"zcbr53",
"zms53",
Or just the data
awk -v RS='"xyz"' -F\" 'NR>1 {print $2}' file
somename_dsa
zcbr53
zms53
This might work for you (GNU sed):
sed '/"xyz":\s*/!d;s//\n/g;s/^[^\n]*\n//' file
Delete any lines that don't have the required string. Replace the required string with a newline and chop off the first entry.
You may try this,
$ cat rr.txt
"xyz": "somename_dsa", some other text, "xyz": "zcbr53", some other text, "xyz": "zms53"
$ sed -r '/[^"]*"xyz": ([^,]*)/ s//\1 /g' rr.txt
"somename_dsa" "zcbr53" "zms53"
$ sed -r '/[^"]*"xyz": "([^"]*)"/ s//\1 /g' rr.txt
somename_dsa zcbr53 zms53
All the captured texts are separated by spaces.

replace the last third line of a file using awk and sed

I need to find the last third line of a file and if its "}," I should replace it with "}"
I was thinking of awk and sed
This finds the last third line of file
PATTERN=$(awk '{v[c++]=$0}END{print v[c-3]}' $file)
I wanted to use
sed -i 's/$PATTERN/}/' $file
But this is wrong
Don't know how to proceed
I need to find the last third line of a file and if its "}," I should
replace it with "}"
Using tac and sed:
tac filename | sed '3s/^};$/}/' | tac
On MacOS, you could say:
sed '1!G;h;$!d' filename | sed '3s/^};$/}/' | sed '1!G;h;$!d'
This awk should do it (last third line more is cleaned)
cat file
one
two },
three
four
more }, test }, hi
five },
yes
awk '{v[++c]=$0}END {for (i=1;i<=NR;i++) {if (v[c-3]~"},") gsub(/},/,"}",v[c-3]);print v[i]}}' t
one
two },
three
four
more }, test }, hi
five },
yes

Unix - Removing everything after a pattern using sed

I have a file which looks like below:
memory=500G
brand=HP
color=black
battery=5 hours
For every line, I want to remove everything after = and also the =.
Eventually, I want to get something like:
memory:brand:color:battery:
(All on one line with colons after every word)
Is there a one-line sed command that I can use?
sed -e ':a;N;$!ba;s/=.\+\n\?/:/mg' /my/file
Adapted from this fine answer.
To be frank, however, I'd find something like this more readable:
cut -d = -f 1 /my/file | tr \\n :
Here's one way using GNU awk:
awk -F= '{ printf "%s:", $1 } END { printf "\n" }' file.txt
Result:
memory:brand:color:battery:
If you don't want a colon after the last word, you can use GNU sed like this:
sed -n 's/=.*//; H; $ { g; s/\n//; s/\n/:/g; p }' file.txt
Result:
memory:brand:color:battery
This might work for you (GNU sed):
sed -i ':a;$!N;s/=[^\n]*\n\?/:/;ta' file
perl -F= -ane '{print $F[0].":"}' your_file
tested below:
> cat temp
abc=def,100,200,dasdas
dasd=dsfsf,2312,123,
adasa=sdffs,1312,1231212,adsdasdasd
qeweqw=das,13123,13,asdadasds
dsadsaa=asdd,12312,123
> perl -F= -ane '{print $F[0].":"}' temp
abc:dasd:adasa:qeweqw:dsadsaa:
My command is
First step:
sed 's/([a-z]+)(\=.*)/\1:/g' Filename |cat >a
cat a
memory:
brand:
color:
battery:
Second step:
sed -e 'N;s/\n//' a | sed -e 'N;s/\n//'
My output is
memory:brand:color:battery: