How do you change column name in csv file with linux on Mac - linux

Simply just want to change few column names in a csv file and store it as it is using this code:
sed -i -e "1s/oldcolname/" -e "1s/newcolname/" xxx.csv.
But it does not work. I got the error message :
sed: 1: "1s/oldcolname/":unterminated substitute in regular expression.
Anyone knows how to rewrite it? Thanks!

If you want to simply replace oldcolname with newcolname, here's a quick answer:
sed -i -e 's/oldcolname/newcolname/' your_file.csv

Related

How to set sed command correctly

When I try following command, I'd like to rewrite sql.
Day='2020/12/1'
Dir=/home/test/data
sql=`cat $Dir"/"$test".sql" | sed -e "s/Day/$Day/g"`
I suffered following errors.
sed: -e expression #1, char 24: unknown option to `s'
Why the s is recognised as option ? why is this command couldnt work well ?
if someone has opinoin, please let me know
Thanks
The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g", containing way too many slashes.
Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string like below:
sql=`cat $Dir"/"$test".sql" | sed -e "s|Day|$Day|g"`
Also, you would need to use sed -i to update the file in-place, since it looks like that is what you're trying to do.

Replace String using Sed?

I am new to Unix and Linux and am trying to replace a certain strings in a file using sed.
This is what I have so far:
Param1=CHANGEME
Param2=Value2
Param3=CHANGEME
Param4=Value4
If I do this command :- sed -i 's/CHANGEME/VALUE/g' input.txt, "CHANGEME" will be replaced by the text "VALUE" which is fine.
What if I want to change "Value" with it's corresponding value number so essentially for Param1=Value1 and for Param3=Value3 ?
How can I achieve that? Thank you
Using GNU sed ...
Your snippet saved to hugger:
cat hugger
Param1=CHANGEME
Param2=value2
Param3=CHANGEME
Param4=value
The following sed command does what you want, I think:
sed -r '/CHANGEME/s/Param([0-9]+)=.*/Param\1=Value\1/' hugger
Param1=Value1
Param2=value2
Param3=Value3
Param4=value4

Not getting replaced using the sed using the sed -i command

I am trying to do the following thing using the sed
cp src/config/template.js src/config/index.js
export API_GATEWAY_ENDPOINT="http://localhost:9000/"
sed -i "" "s|{{API_GATEWAY_ENDPOINT}}|$API_GATEWAY_ENDPOINT|g" src/config/index.js
But when I run this script, then I am getting this error,
sed: can't read s|{{API_GATEWAY_ENDPOINT}}|http://localhost:9000/|g: No such file or directory
I am using a Linux machine.
Can any tell me why this is happening?
The first argument to sed should be the script. So when you have that extra "", the second argument is interpreted as a file name.
And you don't need to copy the file and then do an inplace replacement. Instead use > operator to redirect the output from sed to the proper file.
>src/config/index.js <src/config/template.js sed "s|{{API_GATEWAY_ENDPOINT}}|$API_GATEWAY_ENDPOINT|g"

Sed how to find and replace a value using a bash variable [duplicate]

I have a configuration file (gpsd.default) containing data with the following format:
# If you must specify a non-NMEA driver, uncomment and modify the next line
GPSD_SOCKET="/var/run/gpsd.sock"
GPSD_OPTIONS=""
GPS_DEVICES=""
I am making a change on the file with sed:
sed -i 's/^GPS_DEVICES="".*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default
or
sed -i '4s/^.*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default
The above sed command returns error:
sed: bad option in substitution expression
Because the new line contains "/" in its expression.
How to update my sed command to make it work?
This is because you are using a regex containing /, which is the same character sed uses as delimiter.
Just change the sed delimiter to another one, for example ~:
sed -i 's~^GPS_DEVICES="".*~GPS_DEVICES="dev/ttyUSB1"~' /etc/default/gpsd.default
By the way, since you are changing files in /etc, you may want to use -i.bak, so that the original file gets backed up. It is a good practice to prevent loss of important information.
You should update your sed command to this.
sed -i 's/^GPS_DEVICES=\"\".*/GPS_DEVICES=\"dev\/ttyUSB1\"/' /etc/default/gpsd.default

Sed error : bad option in substitution expression

I have a configuration file (gpsd.default) containing data with the following format:
# If you must specify a non-NMEA driver, uncomment and modify the next line
GPSD_SOCKET="/var/run/gpsd.sock"
GPSD_OPTIONS=""
GPS_DEVICES=""
I am making a change on the file with sed:
sed -i 's/^GPS_DEVICES="".*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default
or
sed -i '4s/^.*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default
The above sed command returns error:
sed: bad option in substitution expression
Because the new line contains "/" in its expression.
How to update my sed command to make it work?
This is because you are using a regex containing /, which is the same character sed uses as delimiter.
Just change the sed delimiter to another one, for example ~:
sed -i 's~^GPS_DEVICES="".*~GPS_DEVICES="dev/ttyUSB1"~' /etc/default/gpsd.default
By the way, since you are changing files in /etc, you may want to use -i.bak, so that the original file gets backed up. It is a good practice to prevent loss of important information.
You should update your sed command to this.
sed -i 's/^GPS_DEVICES=\"\".*/GPS_DEVICES=\"dev\/ttyUSB1\"/' /etc/default/gpsd.default

Resources