Sed adds a space instead of tab at end of line
The objective is to read a file line by line, add a tab at end of each line and add a value(number) after the tab.
My script: Code:
i=0 Code:
Test Line 1 On the other hand each line of Report.tsv gets appended by tab at the end of second run onwards. This was realized when Report.tsv was imported in open office spreadsheet. First set of appended values get merged into the original column (Strings) and the subsequent appended values fall in distinct columns. What is wrong here? Please guide. Thanks in advance. |
Sed is a line editor. A line will end with a new-line. Using "s/$/\t/" will only add a tab before the end of the line.
You can build up lines and then change all of the newlines (except the last) to tabs. This is usually done by adding a line to the Hold buffer; recalling the hold buffer; and performing a global replace "s/\n/\t/". Here is an example, extracting one of the records of the lspci output, and replacing the newlines with tabs: Code:
/sbin/lspci -v | sed -n '/Network controller/,/^$/{ /^$/!H If all you want to do is replace all the newlines in a file with tabs, you could use the `tr' program instead: tr '\n' '\t' <original_file >newfile |
jschiwal,
Really appreciate your quick reply. I didn't quite understand why sed adds a space (not new line) at the end of a line when it has been asked to add a tab. When I run my script third time, first two appended columns get separated by spaces (which were by tabs earlier) and third by a proper tab. This is extremely confusing. Also the same sed command works like a charm if I ask it to add a comma instead of \t at end of each line. The sed-hold buffer example you presented didn't work for me. Get "sed: -e expression #1, char 25: extra characters after command" error. I am researching more on sed and hold buffer. And [tr '\n' '\t'] replaces every single \n by a \t so it isn't really helpful for me. Again, thanks much for the reply. |
Hi,
are you sure that it is not a setting in OpenOffice that malforms the file? Can you post the output of the following command Code:
od -c Report.tmp.tsv |
Tabs need to be quoted to survive being echoed
Code:
line=abc$'\t'123 # $'\t' is a tab character in bash |
I cut and pasted my posted example. It didn't have an error.
If your file is highly structured, consider using awk instead of sed. However, using tabs as record separators instead of field separators is very odd. Normally, tabs separate fields in a record, and newlines separate records. Also, put your sed commands in double quotes if you use bash variables. An alternative is to enclose fixed text in single quotes, and variables in double quotes. You need to do the latter if you use `$' in a sed command meaning end of line. Code:
head kmenu.trace | sed "s/^/$Date\t/" |
Quote:
Output of Code:
od -c Report.tmp.tsv Code:
0000000 T e s t L i n e o n e \t 1 4 Following is the out put of Code:
od -c Report.tmp.tsv Code:
0000000 T e s t L i n e o n e 1 4 Thanks. |
Quote:
I'm indeed on bash. Wrote this script to test: Code:
line="Temporary line number one" Code:
Temporary line number one If I wrap $some by more than one set of quotes (") all the tabs are lost including the last one. Wonder how can I solve this easily. Thanks. |
jschiwal
I completely agree that tabs are normally used to separate fields and new-lines to separate records. This articular report is to be updated on a nightly basis where it should be easy to compare values for subsequent days. Hence the upside down design! Thanks for the pointer to awk and importance of quotes in sed. |
After your post #7 I realize that you are running the script twice on the same file. I initially assumed that you are processing several different files with your script. In this case double-quoting like
Code:
echo "$line" ... |
Following script seems to have done the trick:
Code:
i=0 Code:
0000000 T e s t L i n e o n e \t 1 4 |
Quote:
Example: Code:
echo "" $line "" Code:
echo "$line" Code:
line="\"content of line\"" |
Maybe this can give you some ideas:
Code:
i=0 |
All times are GMT -5. The time now is 08:35 AM. |