How to remove 1st empty column from file using bash commands? - linux

I have output file that can be read by a visualizing tool, the problem is that at the beginning of each line there is one space. Because of this the visualizing tool isn't able to recognize the scripts and, hence crashing. When I manually remove the first column of spaces, it works fine. Is there a way to remove the 1st empty column of spaces using bash command
What I want is to remove the excess column of empty space like shown in this second image using a bash command
At present I use Vim editor to remove the 1st column manually. But I would like to do it using a bash command so that I can automate the process. My file is not just full of columns, it has some independent data line

Using cut or sed would be two simple solutions.
cut
cut -c2- file > file.cut && mv file.cut file
cut cannot modify a file, therefore you need to redirect its output to a different file and then overwrite the old file with the new file.
sed
sed -i 's/^.//' file
-i modifies the file in-place.

I would use
sed -ie 's/^ //' file
to just remove spaces (in case a line does not contain it)

Related

Copy lines from one file to another in Linux excluding comments

How do I copy lines from one file to another in Linux without opening source and destination files and I need to exclude the comments when copying the lines.
I do not want to copy the comments in the first file and the files are in different locations
Assuming lines are commented with # at the very beginning of each line, the following should work:
grep -v "^#" path/to/input/file >path/to/output/file
(Note: This will either create a new output file or irreversibly overwrite the output file if it already exists)
Assuming comment lines in your file contain # at the beginning of each line, the following sed command will delete these lines:
$ sed '/^#/d' path/to/input-file > path/to/output-file
If your file can also contain lines with whitespace before the #, the following sed command will delete lines beginning with zero or more spaces or tabs (in any order), followed by a hash (#) character:
$ sed '/^[ \t]*#/d' path/to/input-file > path/to/output-file
If your file also contains lines containing code followed by a comment, the following sed command should work:
$ sed -e '/^[ \t]*#/d' -e 's/#.*$//' path/to/input-file > path/to/output-file

Bash deleting a specific row in .dat file

So, I have this assignment which requires me to delete a certain line from a .dat file. Now my file is basically a phone book. I have a Bash script that adds the ID, name, last name, phone number, address, etc., to the .dat file. Now one of the flags is supposed to be -delete and it takes the parameter id. So, basically I need to implement the function where I'd put ./phonebook.sh -delete -id 7 and it would delete the row where the id is 7.
I tried using sed and awk, but nothing is working and it's frustrating. My current code for that short script (delete.sh) is:
id=$1
sed "/$id/d" phonebook.dat
Try this:
On Mac:
sed -i '' -e "/$id/d" phonebook.dat
Otherwise:
sed -i -e "/$id/d" phonebook.dat
By default, sed will output the results to stdout. So, your command was working, but the output wasn't going back into the file. The -i flag says that the file should be replaced with the results. -i is also meant to backup the original file. For example:
sed -i .bk -e "/$id/d" phonebook.dat
The above will create a copy of the original called: phonebook.dat.bk. However, to do in place replacement without a backup, you can specify no value for -i. On the MAC, sed really really really wants a value, so you can pass it an empty string ( making sure there is a space between the -i and the empty quotes ).
I'm making some assumptions because I don't know what the format of your dat file is. I'll assume that the id field is the first field and the file is comma delimited. If I'm wrong, you should be able to modify the below to fit your needs.
I personally like to use grep -v for this problem. From the --help:
-v, --invert-match select non-matching lines
Running this will output every line of a file that does not match your pattern.
id="$1"
grep -v "^${id}," phonebook.dat > phonebook.temp
mv phonebook.temp phonebook.dat
The pattern consists of
^: Beginning of the line
${id}: Your variable
,: Our assumed delimiter
The reason for specifying the beginning of the line to the first delimiter is to avoid deleting entries where the entered id ($1) is a substring of other ids. You wouldn't want to enter 22 and delete id 22 as well as id 122.

use sed to delete patterns on a range of lines

I have a large text file that I would like to divide into segments and use sed to delete certain patterns in place. I would like to do this in a single command line using a pipe. For example:
sed -n 1,10p <text file> | sed -i 's/<pattern to remove>//'
The code above attempts to take the first 10 lines of the text file and remove the patterns from the 10 lines in place. The resulting text file should have the first 10 lines modified. The code above doesn't work because the second command after the pipe requires a input file. Please help!
Something like
sed -i '1,10s/pattern//' foo.txt
though for in place editing of a file I prefer ed or perl instead of relying on a non standard sed extension like -i.
This seems to do what you're asking ....
sed -ni '1,10s/pattern//p' file

How to edit string at a line in file in linux

I have file which contains text at line 30 which is:
Icon="\<some path which we do not know\icon.png"
I want to replace above with:
Icon="\home\user\Img\Icons\icon.png"
What is the best way to do it.?
Thanks.
Best way:
perl -pi -e 's/\\<some path which we do not know/\\home\\user\\Img\\Icons/' text.txt
Perl approach is more preferred than sed, because of Unix compatibility.
You can either use an editor to do this manually or if you prefer to do it non interactively, you can use a small shell pipeline and sed.
sed `3 s/big path/custom path/` input_file.txt
where 3 is the line number, big path is what you want to replace and custom path is what you to want to replace it with. input_file.txt is your input file. This will print the replaced file onto the screen which you can redirect into another file using the > operator.
As a concrete example, suppose I have this file (input_file.txt)
Header
Random test
/bad/path/to/some/directory/icon.png
/bad/path/to/some/directory/icon.png
Footer
Now I'm going to run my command like so.
cat input_file.txt | sed '4 s/\/bad\/path\/to\/some\/directory\//\/home\/noufal\//'
and I get
Header
Random test
/bad/path/to/some/directory/icon.png
/home/noufal/icon.png
Footer
Notice that it has changed only the 4th line. The extra \ characters in the command are to escape the / character which has special meaning for sed.
you can use vim to find and replace your strings http://vim.wikia.com/wiki/Search_and_replace or use 'sed' command

How to remove certain lines of a large file (>5G) using linux commands

I have files which are very large (> 5G), and I want to remove some lines by the line numbers without moving (copy and paste) files.
I know this command works for a small size file. (my sed command do not recognize -i option)
sed "${line}d" file.txt > file.tmp && mv file.tmp file.txt
This command takes relatively long time because of the size. I just need to remove the first line and the last line, but also want to know how to remove line number n, for example.
Because of the way files are stored on standard filesystems (NTFS, EXTFS, ...), you cannot remove parts of a file in-place.
The only thing you can do in-place is
append to the end of a file (append mode)
modify data in a file (read-write mode)
Other operations must use a temporary file, or temporary memory to read the file fully and write it back modified.
EDIT: you can also "shrink" a file as read here using a C program (Linux or Windows would work) so that means that you could remove the last line (but still not the first line or any line in between)
you can use the ed command which is quite similar to sed
ed -s file.text
you can use the d command, $d will delete the last line while 1d will delete the first one, and wq will write and exit.
The following command will do everything (delete first and last line, write, and exit)
echo -e '1d\n$d\nwq' | ed -s test.txt
using sed you can use the same commands sed '1d;$d' test.txt
If you are using a recent Linux, you can remove chunks of the file in any position: https://lwn.net/Articles/415889/
There's a command to remove any part of the file: fallocate
See: https://manpages.ubuntu.com/manpages/xenial/man1/fallocate.1.html
For example: fallocate -p -o 10G -l 1G qqq

Resources