add square brackets in each line of the file using bash - bash

I would like to add square brackets to each and every line of my file.
I was using sed -i "s/\(.*\)/[\1]/" file_name.txt command but I see that it is inserting 2 times the square brackets and my output (just the first line and it's the same for every single line) looks like this
[[1,1,0,0,0,0,1,0,24,3,0,0,0,0,86,149,149,14,0,0,0,0,32,149,46,16,0,0,1,13,3,33,65,66,0,0,0,0,0,2,149,140,6,0,0,2,62,148,88,24,26,2,0,14,116,148,30,15,1,0,0,1,5,30,56,18,0,0,0,0,0,4,149,46,40,14,0,0,1,34,31,46,149,31,0,2,9,12,1,7,8,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,12,2,0,0,0,0,0,0,0,0,0,0,0,0]]
How do I insert just one single square bracket for each line using sed? Please help. Thanks in advance!

Using awk:
awk '{print "["$0"]"}' file_name.txt

your command is fine and should not add 2 [ unless runnning twice. Also group, in this case, in not necessary, taking the whole pattern & is suffisant
sed -i 's/.*/[&]/' file_name.txt
Maybe, try with a single quote your own, double could aways be interpretated by something else from the shell

Related

Delete double quotes in csv bash

I’m having the following issue:
I have the csv file with data inside looks like this:
“1,””name””,””surname””,””age””,””city”””
“2,””Peter””,””Parker””,””30””,””NY”””
“3,””marry””,””Jane””,””30””,””NY”””
Is it possible using bash to delete the first and the last double quote from each row and then first and last double quote from each field in it?
To get something like this:
1,”name”,”surname”,”age”,”NY”
3,”marry”,”Jane”,”30”,”NY”
I would be grateful for some hints. Thanks
To get you started:
echo '"1,""name"",""surname"",""age"",""city"""' | sed "s/\"\"/\"/g" | sed 's/^\"\(.*\)\"$/\1/'
OUPUT
You can take that, adjust it to run over a file, line by line (instead of the first echo and output into another file
Presuming your input looks like this:
"1,""name"",""surname"",""age"",""city"""
"2,""Peter"",""Parker"",""30"",""NY"""
"3,""marry"",""Jane"",""30"",""NY"""
Note the actual "'s not the ”” in your code:
You can then sed multiple things and chain them together e.g.
sed -e "s/\"\"\"/\"/g" -e "s/\"\"/\"/g" input.txt
This first replaces the triple quotes """, reducing them to double quotes "" and then reduces them further.
Final output:
"1,"name","surname","age","city"
"2,"Peter","Parker","30","NY"
"3,"marry","Jane","30","NY"
If you have special characters then simply replace them in the code e.g.:
$ cat input.txt
“1,””name””,””surname””,””age””,””city”””
“2,””Peter””,””Parker””,””30””,””NY”””
“3,””marry””,””Jane””,””30””,””NY”””
$ sed -e "s/\”\”\”/\”/g" -e "s/\”\”/\”/g" input.txt
“1,”name”,”surname”,”age”,”city”
“2,”Peter”,”Parker”,”30”,”NY”
“3,”marry”,”Jane”,”30”,”NY”
Though I think this input is a transpose error in your question.
Using sed:
sed 's/^"\(.*\)"$/\1/;s/"\+/"/g' file
The first substitution removes the outer double quote on the whole line.
The second substitution replaces the parameter quote to only one double quote.

Create polymorphic bash without die trying. Sed replacing

I'm creating a bash script which in one point needs to modify itself in order to make persistent a change (only one line) of the script needs to change.
I know sed -i is what I need to do this. The problem is my sed command is replacing the line where the command is stored instead of the line I want. So I guess I need to include an exclusion while replacing. Let's check the snippet code stuff:
#!/bin/bash
echo "blah,blah,blah"
echo "more code here, not matters"
sed -i "s/#Awesome line to be replaced/#New line here/" "/path/to/my/script" 2> /dev/null
#Awesome line to be replaced
echo "blah,blah,blah, more code blah"
The problem here is the replaced line is not the line with only #Awesome line to be replaced. It is replaced the line where the sed command is.
This is a reduced example but the script is polymorphic and maybe the line numbers change, so it can't be based on line numbers. And there will be more sed commands like this... so I thought It could be nice to have some piece of text which always could be in the sed command lines in order to use it as excluding pattern, and yeah! that piece of text is /dev/null which always will be in sed command lines and never in the line which I want to replace.
How can achieve this using sed -i? Thanks in advance.
EDIT Forgot to say the order of appearance (offset) can't be used neither because of the polymorphic thing.
EDIT2 Beginning chars before #Awesome line to be replaced can't be used because they could change too. Sorry for who already answered based on this. Is complicated to write a polymorphic snippet considering all the possibilities.
This hack can work:
sed -i "s/#[A]wesome line to be replaced/#New line here/" "/path/to/my/script" 2> /dev/null
I think it is self-explanatory, why it will not match the sed line itself.
Anchor your expression by starting your sed line with :
sed -i "s/^$'\t'*#Awesome (rest of command goes here)
This will make sure sed only matches if the text found is at the beginning of the line with zero or more tabs, and will not match the line with the actual sed command.

bash - sed doesn't replace all semicolons

I have three variables I write into a text file. For now it is into a variable, because try and error is faster like this. These three variables are produced by the script before and each variable has a column with values in them. Variable one for example looks like this:
hour
minute
minute
day
I put them together using this code:
New_fileloc=$(paste <(echo "$Grabinterval") <(echo "$Filelocation") <(echo "$Time") --delimiters ';' | sed -e 's/^\|$/"/g' -e 's/\;/";"/')
At the end I need each line in each column in double quotes and separated by a semicolon. At the moment I am doing that using sed in that one liner. And that works mostly fine. My output looks like this:
"hour";"A_path/to/somewhere/;2016-02-10 17:07:00Z"
"minute";"A_path/to/somewhere/;2016-01-29 17:26:20Z"
"minute";"A_path/to/somewhere/;2016-01-29 17:26:20Z"
"day";"A_path/to/somewhere/;2016-01-29 00:07:00Z"
The first semicolon gets replaces with ";", but the second one in each line does not. I have no idea why.
In your last command to sed you are missing a g
s/\;/";"/g
Your original command with 's/\;/";"/' will only make one replacement, the first.

Removing chars until a numeric is found from array item Bash

I have the line of text within a text file. The line looks something like this:
xxxx,xxxxx,xxxxxx,xxxxx,xxxx,NL-1111 xx,xxxx,xxx
The NL- is an identifier for the country so this could be anything. I would like to remove the NL- part from the line so it looks like this:
xxxx,xxxxx,xxxxxx,xxxxx,xxxx,1111 xx,xxxx,xxx
And write the file afterwards.
Thanks in advance.
Another solution close to sed's ones, but with perl:
perl -i -pe "s/(?<=,)[a-zA-Z]{2}-//g" file.txt
It uses look behind expression, so that you don't need to repeat the comma in the replacement part.
something like this using sed
sed -i 's/,[A-Z][A-Z]-\([0-9]\+,\)/,\1/i' file.txt
,[A-Z][A-Z]-\([0-9]\+,\)search for comma letter, letter, -, digit(s), comma
,\1keep only the commas and the digits.
iignore case on the letters
thankyou to #chris for proof-reading.
I think the simplest solution here is reading it from the file into a shell variable, then writing it back immediately and using the pattern substitution variation of parameter expansion:
line="$(<file)"; echo "${line/[a-zA-Z][a-zA-Z]-}" >|file;
I would warn you against solutions that use sed-in-place functionality. I've found that sed behavior differs on different platforms with respect to the -i option. On Mac you have to give an empty argument ('') to the -i option, while on Cygwin you must not have an empty argument following the -i. To get platform compatibility you'd have to test what platform you're on.
sed might do the trick: remove the string ",NL-", "BE-" etc from anywhere in the file:
sed -i 's/,[A-Z][A-Z]-/,/' file.txt

sed copy substring from fixed position and copy it in front of line

I'm dealing with many csv files and I can't find a way with sed to select a substring at a fixed position (chars 9-16) and copy it at the beginning of the line.
This is what I have:
ABC09638006924340017;SOME_TEXT;SOME_OTHER_TEXT
This is what I need:
00692434;ABC09638006924340017;SOME_TEXT;SOME_OTHER_TEXT
The following code in sed gives the substring I need (00692434) but overwrites the whole line:
sed 's/^.{8}(.{8}).*/\1/')
I'm already using sed to "clean" the linestrings and inserting some variables, called in a bash script that at the end imports data in postgres. This is why I would prefer to remain within sed, but any hint will be greatly appreciated as I'm not a real expert.
You need to escape the curly braces (\{\}) as well as the parentheses (\(\)) and also append the original string (&) in the replacement:
text="ABC09638006924340017;SOME_TEXT;SOME_OTHER_TEXT"
echo $text | sed "s/^.\{8\}\(.\{8\}\).*/\1;&/"
Output:
00692434;ABC09638006924340017;SOME_TEXT;SOME_OTHER_TEXT
Since you want to extract a fixed-length substring at a fixed position, you could also do this with just bash-builtins:
text="ABC09638006924340017;SOME_TEXT;SOME_OTHER_TEXT"
echo "${text:8:8};$text"
This migth work for you (GNU sed):
sed -r 's/^.{8}(.{8})/\1;&/' file

Resources