|
|
ScriptsWelcome script-people. So here is a log of some useful scripts I have made.
Change a character delimited file to a new-line delimited file.The other day, I was using UNIX and I had 2 files that I wanted to compare with diff. The files were pipe (|) delimited. Each file was 1 line of data. Diff showed the whole line as being different between the files which was true. I wanted to see which data elements were different, so I made a script with sed to convert the pipes to new line characters. This was a lot harder than it should be. Sed is supposed to be great for replacing things. Anyway here's the script: The input file: a|b|c|d||| 1|2|3|4||| The script: #!/bin/ksh
output="${1}2"
cat ${1} | \
sed -n 's/|\(.\)/\
\1/pg' > $output
The output file: a b c d || 1 2 3 4 || So, I was thinking, this script kind of sucks and it was really hard to make, and I don't even understand exactly why it works. Then I decided to see if I could do it using Perl. Here's the Perl version. When you run it, you can use ">" to redirect the output to a file. The input file: a|b|c|d||| 1|2|3|4||| The script: $input=shift;
open (IN, "$input") || die $!;
$string="X";
while (<IN> )
{
$string = $_;
$string =~ s/\|/\n/g;
print $string;
}
close (IN);
The output file: a b c d 1 2 3 4 The end. |
|