This afternoon I asked a question to see how to make a graph using gnuplot. I leave the link here:
Got a pretty good answer from user abulafia. What I have done, thanks to the response of this user, has been a bar graph where you can see a number of countries and some values. In his response he told me that in order to graph the countries, those that have spaces should be between quotes . That is, rescuing the post file that I made, these are part of my data:
Albania 1970
Antigua and Barbuda 11
Argentina 82219
Armenia 1905
Aruba 101
Australia 70111
Austria 50073
Azerbaijan 1656
Bahamas 93
Bahrain 463
And abulafia told me that they should be like this:
Albania 1970
"Antigua and Barbuda" 11
Argentina 82219
Armenia 1905
Aruba 101
Australia 70111
Austria 50073
Azerbaijan 1656
Bahamas 93
Bahrain 463
So my question is, how can I get an output where countries that have spaces appear in quotes?
I have tried the following with awk:
awk -v OFS="'" '{print "", $1, $2""}' fichero.txt | head -10
But with this I can only put the first word in quotes:
'Albania'1970
'Antigua'and
'Argentina'82219
'Armenia'1905
'Aruba'101
'Australia'70111
'Austria'50073
'Azerbaijan'1656
'Bahamas'93
'Bahrain'463
As you can see, in the country Antigua and Barbuda I get 'Antigua' and. This happens to me with the rest of the countries with spaces.
I have also tried with sed but I only get the whole row between quotes to be printed:
sed 's/^/"/;s/$/"/' suicidiosxpais.txt | head -10
"Albania 1970"
"Antigua and Barbuda 11"
"Argentina 82219"
"Armenia 1905"
"Aruba 101"
"Australia 70111"
"Austria 50073"
"Azerbaijan 1656"
"Bahamas 93"
"Bahrain 463"
For now, rather than focusing on those countries with spaces, what I am trying to do is put quotes around all the countries in the file, because I understand that if they all have quotes it will also be possible to make the graph.
Any idea how to do it?
Thank you!!
Another alternative with
sed
is:So I just look for all the characters before a space
(.*)
(sed
assigns them to group 1), then a string with several numbers that are before the end of the line([[:digit:]]+)$
(sed
assigns them to group 2).Next, we tell
sed
it to print the first group enclosed in quotes"\1"
, followed by a space after group two (the number)\2
.Another similar option but with
gawk
is:How does it work:
last=$NF
I catch the last word of the line$NF=""
I delete that last word, so in$0
, which represents the line, I am left with only the name of the country (and a trailing space)gsub(/ $/,"")
I delete the trailing space of$0
print
I print the$0
in quotes and then, after a space, the word that I had captured inlast
.Input example:
Produces the output: