OK. It’s been a very long time since I used any of linux’s command line text utilities. this is what I want to do.
For every *.dat (text file) in a folder, I want to read the first line and append it to another file, say consolidate.txt, read the next file and append the first line, and so on. Consolidate.txt (also a text file) will have 1 line in it to begin with, which will be the titles to each column for the next operation.
Then I need to replace every “|” with a “,” in consolidate.txt. Then I need to load it into Open Office Spreadsheet.
Any suggestions how I should do this would be appreciated.
There are probably more than 1 way to achieve the first part. I could concatenate all the files and then delete every line beginning with “Unused” in Consolidate.txt.
I am certain that what you want to do can be done with bash scripting, a text file that includes a sequence of terminal type command, ran one after the other. You can search the internet for the phrase like “Linux Shell Scripting with Bash” and come up with all sorts of hits. Here is just one example:
Linux Shell Scripting Tutorial - A Beginner’s handbook
Now here is an example script file that I wrote which does not do what you want, but shows how such a file is written:
# Script file to be used with a file manager like Dolphin to allow a text file edit with root permissions, but only when required.
# make sure we got file-name as command line argument
if $# -eq 0 ]
echo "$0 file-name"
which stat > /dev/null
# make sure stat command is installed
if $? -eq 1 ]
echo "stat command not found!"
# make sure file exists
if ! -e $FILE ]
echo "$FILE not a file"
OWNER="$(/bin/stat -c %U $FILE)"
if "$OWNER" != "root" ] ; then
kdesu kwrite $FILE
# End Of Program
This bash text file was written to allow you edit text files with a file manager, using root access on a file only if it belongs to root, thus not changing its owner when it does not belong to root.
All bash text files must be marked as executable and can end in .sh if you like, to show they are script files, though this is not required.
The big deal here is that script files have extensive text search and replace abilities and will very likely be able to do what you want.
Tell you what, I’ll give you the script, and your learning exercise will be to reverse engineer why it does what it does:
for i in *.dat
head -1 "$i" | sed 's/|/,/g'
done >> consolidate.txt
Actually on closer reading of the head man page, it turns out it can handle multiple files, but you must use the -q flag to avoid outputting the file name as header. So it’s a one-liner:
head -q -1 *.dat | sed 's/|/,/g' >> consolidate.txt
and because sed can also handle the first line only stipulation, you can eliminate the use of head.
sed -n -s -e '1s/|/,/g' -e 1p *.dat >> consolidate.txt
but you have to read the man page to understand why the -s.
Oh no, no, no! You’re not getting off that easy! We want a report on the reverse engineering of ken’s script! Each and every character!