How to delete first column from the file in Unix [duplicate] - shell

This question already has answers here:
Delete a column from a delimited file in linux
(7 answers)
Closed 8 years ago.
I have a file like the one below -
|A|B|C|D
|1|2|3|4
I want the result -
A|B|C|D
1|2|3|4
I have tried using cut but I'm not getting the desired output. Please suggest how the first column can be removed?

Using sed, delete the leading pipe symbol:
sed 's/^|//' file
There's an outside chance that on some versions of sed you'd need to escape the pipe. You might be able to use the over-write mode too (though not all versions of sed support that):
sed -i .bak 's/^\|//' file

Related

How do I remove a line that does not contain a specific character? [duplicate]

This question already has answers here:
In-place edits with sed on OS X
(8 answers)
Closed 2 years ago.
For example, I want do remove all lines in a textile that do not contain the character '#'
I have already tried to use sed like so
sed '/#/!d' data.txt
What am I missing? Shouldn't this work?
I prefer using ed over the non-standard sed -i, especially if it needs to be portable:
printf "%s\n" "v/#/d" w | ed -s filename
This deletes every line that doesn't contain a #, and saves the changed file back to disc.
sed -n '/#/p' [file]
-n suppress default printing
/#/ match on # anywhere on the line
p print if it matches
Add -i for in-place editing of the file (if supplied).

Overwrite a url config file using Sed [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I have a flag on a file
APP_URL=http://localhost
I want to update it to
APP_URL=http://aws.test
I want to overwrite it, I tried
sed -i -e 's/APP_URL=http://localhost/APP_URL=http://aws.test/g' .env
and
sed -i -e 's/APP_URL="http://localhost"/APP_URL="http://aws.test"/g' .env
I kept getting
sed: 1: "s/APP_URL="http://local ...": bad flag in substitute command: 'l'
How would one go about debugging this further?
You have too many forwardslashes in your command. Either escape the ones in the url with \/ or use a different separator for sed, ie:
sed 's#replace/this/string#with/this/one#g'

How to use sed to change references to directories? [duplicate]

This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 8 years ago.
So I know how to use sed -i 's/string/new-string/g' *.* but I'm trying to change all references to a directory structure and I don't know the proper format for the command.
Example: I want to change 'myfolder/mysubfolder' to 'myfolder'.
So if there's a string in a file that says 'myfolder/mysubfolder/file.txt' I want it to say 'myfolder/file.txt'.
You are on right track, just need to escape the /
sed -i 's/myfolder\/mysubfolder/myfolder/g' *.*
sed -i 's#myfolder/mysubfolder#myfolder#g' *.*
I used a delimiter other than / to avoid having to escape the / in the string.

sed won't delete what I want [duplicate]

This question already has answers here:
Find and replace in file and overwrite file doesn't work, it empties the file
(12 answers)
Sed to delete a range of lines from a specific match line TILL a specific match line (not including last line)
(3 answers)
Closed 8 years ago.
I am writing a bash shell script, and in it I am trying to delete lines from a text file between 2 markers
START
...
...
END
Do eliminate this I have tried a few things, and every time it leave my text file blank.
sed '/START/,/END/d' myfile.txt > myfile.txt
sed '/START/d' myfile.txt > myfile.txt
As long as I have a sed command in my code, my entire file gets erased and not just the section I am looking to erase.
You are redirecting stdout to the same file as stdin. When you do this, your redirection is interpreted buy the shell and it opens a new file for write with that name. Your existing file is overwritten by the newly created blank file. You will need to redirect the output to a different file or you can edit the file by using the -i option to sed.
You can't redirect to a file that you are reading. That will delete your file's contents, as you've noticed.
Instead, either redirect to a different file, or edit in place:
sed -i ... myfile.txt
A way in awk.
awk '/START/{x=1}/END/{x=0}!x{print > ARGV[1]}' myfile.txt
Portable
awk '/START/{x=1}/END/{x=0}!x{print > "myfile.txt"}' myfile.txt

Can I search/replace in multiple .txt files quickly from Terminal? [duplicate]

This question already has answers here:
Recursive search and replace in text files on Mac and Linux
(14 answers)
Closed 7 years ago.
I have about 100 .txt files that contain plain text. Somehow, some of the data has been corrupted and needs to be found/replaced.
I need to search for the characters'--' and replace it with a long dash: '—'.
Is there a way to do this quickly with a command in terminal?
The names of the .txt files in my directory are numbered sequentially: 1.txt, 2.txt, etc.
Thanks!
GNU sed:
sed -i 's/--/—/g' *.txt
OSX BSD sed:
You need to specify a backup file extension. To create a backup file with the extension: .txt.bak:
sed -i '.bak' 's/--/—/g' *.txt
To completely replace the files, specify an empty extension:
sed -i '' 's/--/—/g' *.txt
sed -i 's/--/–/g' *.txt ought to work. The -i flag to sed makes it act on the files in-place, the s stands for substitute, and the g makes it replace multiple occurrences of the pattern on the same line. Look up sed's documentation for more information.
EDIT: This works on GNU/Linux; it turns out that the syntax is slightly different on OSX (see comments and accepted answer).

Resources