I am having trouble loading a space delimited text file into a table. The data in this text file is generated by teragen, and hence, is just dummy data, where there are only 2 columns, and the first column has values of random special character strings.
Example:
~~~{ZRGHS|
~~~{qahVN)
I run into a problem and get rejected rows because some of these values have a space in them as a random ASCII character, which causes it to think that there are 3 columns, when my table has 2, so they get rejected.
So, what I want to do is remove only the first space from these rejected rows, which will need to be repeated multiple times over each row, and then try to reload them. Would sed be the best way to go about this, or would something else like tr be more appropriate?
Thanks!
With GNU sed's -z option you could process the whole file as if it was only one line. That way a s/…/…/ would only replace the first match in the whole file. Remember: s/…/…/ only replaces the first match in each line, but with the -z option sed treats the whole file as a single line.
Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.
Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line. Output : linux is great os.
From what I understand, you want to remove all spaces except the last two.
You can build a regex for that, or you could use the fact that it's very easy to keep the first n occurrences:
$ echo 'one two three four' | rev | sed 's/ //2g' | rev
onetwothree four
or, with a file:
rev myfile | sed 's/ //2g' | rev
Or you could remove one space until there is only one space left:
$ echo 'one two three four' | sed ':a;/ .* /{s/ //;ba}'
onetwothree four
with a file:
sed ':a;/ .* /{s/ //;ba}' myfile
Or, if you're in the mood, you can split the line, play with it, and assemble it back (GNU sed assumed):
$ echo 'one two three four' | sed -r 's/(.*)([^ ]+) ([^ ]+)$/\1\n\2 \3/;h;s/\n.*//;s/ //g;G;s/\n.*\n//'
onetwothree four
with a file:
sed -r 's/(.*)([^ ]+) ([^ ]+)$/\1\n\2 \3/;h;s/\n.*//;s/ //g;G;s/\n.*\n//' myfile
To remove the first space from a line, use
echo "my line with spaces" | sed 's/ //'
Depending on the specifics of your approach (fixed column length? how are you adding the data?) there might be a better way to do this in a single step instead of parsing rejected rows over and over.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With