Space delimited with 'ssconvert' - import-from-excel

I want to use ssconvert from Gnumeric to convert an Excel file to .txt file. The command should be:
ssconvert -O 'separator= ' excel.xlsx result.txt
However, it doesn't insert ' ' between cells. For example A B is written as AB.
How can I fix that?

Try that:
ssconvert -O "separator=' '" excel.xlsx result.txt

Related

How to do a custom "grep" in linux terminal

Suppose I have file which contains only a text like below:
Test transition to drned-internal-asr9k-rt24711
load drned-internal-asr9k-rt24711
commit**
Now on the terminal if I do
cat filename | grep load
I would get output something like
load drned-internal-asr9k-rt24711
But how can I modify my grep command to get output as
drned-internal-asr9k-rt24711.txt
i.e. remove "load " and add ".txt" at the end. So how to do that??
May be not the best solution but :
cat | grep load | cut -c4- | sed 's/$/.txt/'
cut -c4- will delete the 4 first characters
sed 's/$/.txt/' will add the ".txt" at the end of output
This can be achieved with the following:
sed -nr 's/.*load\s+(.*)/\1.txt/p' file.txt
This matches anything after load (plus one or more spaces) and returns it, adding .txt on the end.
awk '{for(i=1;i<NF;i++){if(tolower($i)~/^load$/){print $(i+1) ".txt"}}}' file.txt
This matches next column after load and append .txt to it in output.

Matlab execute UNIX command not working in newer versions

I have a script that uses this line of code:
system(['cat ' inputfile ' | tr -d ''\000'' | tr -d ''\015'' >& tempfile.txt']);
to go through a text file and delete some special characters and then put it into a temp file.
This line of code works in Matlab2012 but not in 2017 as it leads to this error:
tr: Illegal byte sequence
cat: stdout: Broken pipe
Does anyone know how to get around this issue? Thank you!
The encoded format may not be supported by tr, try changing the locale (refer to https://unix.stackexchange.com/questions/141420/tr-complains-of-illegal-byte-sequence):
system(['cat ' inputfile ' | LC_ALL="C" tr -d ''\000''''\015'' >& tempfile.txt']);

convert bcp file to standard csv file

I am looking for any known tools or scripts which can convert my bcp files to csv files.
Input bcp file format:
Fields separated by 'XXXXXXX'
Rows separated by 'YYYYYYY'
Fields contains special characters like CRLF,CR,LF,", Tab, comma etc...
Output format I want:
Standard csv format file with comma delimited
Field values should contain original content including special characters (I mean no addition or deletion of special character(s), CR should also not be deleted)
The file able to cut by column index/name to select interested columns.
For this I did the following:
Transformed the bcp file to csv with few sed commands, with this I can open the file in MS excel program with proper alignment, and I could see content was not altered (as expected).
sed -i 's/\"/\"\"/g' $inFile
sed -i 's/XXXXXXX/","/g' $inFile
sed -i 's/YYYYYYY/"\n"/g' $inFile
sed -i '1s/^/\"/' $inFile
sed -i '$s/\"//' $inFile
sed -i -e '${/^$/d}' $inFile
sed -i '1s/^/"Header","added","here"\n/' $inFile
Tried csvkit tool: csvcut $infile
This tool is selecting the preferred columns but modifying the content like deleting the CR.
Any ideas in this kind conversion?
awk to the rescue!
awk -F='XXXXXXX' -v RS='YYYYYYY' -v OFS='","' -v ORS='\n'
'{gsub(/"/,"\""); $1=$1; print "\""$0"\""}' file
escape quotes, quote fields, change field delimiter to comma and record delimiter to newline. $1=$1 forces awk to apply the new delimiters.

how to use the name of the input file in sed replace

i have several files in which i want to replace a certain word with the name of the file itself..
for example i have 2 files named test1.txt and test2.txt
both files are equal and look like
bla1,bla2,temp
bla2,bla3,temp
with the sed i want to replace the word temp with the name of the file itself
so after the sed operation i have 2 different files
test1.txt , which looks like :
bla1,bla2,test1
bla2,bla3,test1
test2.txt, which looks like :
bla1,bla2,test2
bla2,bla3,test2
so my question ... how do i use the actual name of the input file itself as part of the replace command?
sed "s/temp/ ??filename??/ ??? " *.txt
thanks for your suggestions
I'm not sure you can reference the filename using sed although I could be wrong. You would probably use a shell hack. A better aproach to substitute all occurrences of temp with the filename would be the following awk script:
$ awk '{gsub(/temp/,FILENAME)}1' file
use awk, awk has FILENAME variable:
awk '{sub(/temp/,FILENAME)}7' yourfile
awk 'BEGIN{FS=OFS=","} {$NF=FILENAME}1' file
The difference between this and the sub() solutions is that this will work even if the word "temp" exists elsewhere in your file, e.g. if "bla1" contains the word "temperature".
If you need to strip ".txt" from the file name as it appears from your posted desired output, tweak it to:
awk 'BEGIN{FS=OFS=","} {t=FILENAME; sub(/\.txt$/,"",t); $NF=t}1' file
You can probably edit FILENAME itself but I find it best not to mess with the builtin variables if you don't have to.
You could do it with a little bit of bash to help you out, if that's available.
find . -name "test*.txt" -type f | awk -F '/' '{print $2;}' | while read file; do sed -i "s|temp|$file|" ./$file; done
That's a kind of hacky adaptation of a script I have to do something similar. It can undoubtedly be shortened.
no sed internal variable for the file name so you need some previous batch command for a generic process
for FileName in MyFileShellFilter
do
cat <> ${FileName} | sed "s|,temp$|,${FileName}|"
done
just be carrefull with file name used, they normaly don't have \ but could have & that are s// special meaning. I use | as separator to allow / in file name but for this reason, no unescaped | are allowed in file name (normaly not)
with xargs:
printf "%s\n" *.txt | xargs -I FILE -L 1 sed 's/temp/FILE/' FILE
The filename cannot have: newlines, slashes, ampersand, single quote.

Unix - Split to N files using regexp to name destination file

How do I split a file to N files using as a filename the first 2 chars on the line.
Ex input file:
AA23409234TEXT
BA23201202Other Text
AA23509234YADA
BA23202202More Text.
C1000000000000000000
Should generate 3 files:
AA.txt
AA23409234TEXT
AA23509234YADA
BA.txt
BA23201202Other Text
BA23202202More Text.
C1.txt
C1000000000000000000
I'm thinking of using a sed script similar to this
/^(..)/w \1
But what that really does is create a file named '\1' instead of the capture group.
Any ideas?
$ awk '{fname=substr($0, 0, 2); print >>fname}' input.txt
Or
$ while read line; do echo "$line" >>"${line:0:2}"; done <input.txt
The first thing you need to do is determine all of your file names:
filenames=$(sed 's/\(..\).*/\1/' listOfStrings.txt | sort | uniq)
Then, loop through those filenames
for filename in $filenames
do
sed -n '/^$filename/ p' listOfStrings.txt > $filename.txt
done
I have not tested this, but I think it should work.
This might work for you:
sed 's/\(..\).*/echo "&" >>\1.txt/' file | sh
or if you have GNU sed:
sed 's/\(..\).*/echo "&" >>\1.txt/e' file