Bash script to convert all files of a given type from Unix to Dos Format - linux

I need to be able to go back and forth between CentOS and Windows for C++ projects that I am developing on both systems. I want to be able to convert all files of a given type such as *.cpp, *.h or *.txt from Unix to Dos.
I have a bash script that converts a single file, but I'd like to be able to type in either just the extension of the file type or *.FILETYPE to convert all the files in a given directory of that type.
Here is the working script, what do I need to do to make it work for more than one file at a time. I've tried using a for loop but I get syntax errors.
#! /bin/sh
# Convert a text based file from Unix Format to Dos Format
# The Output file should be the name of the input file with
# WF inserted before the file extention
#
# Examples
# convert Unix format abc.txt to abcWF.txt Windows Format
# convert Unix format abc.cpp to abcWF.cpp Windows Format
echo "In u2w FileToConvert = $1"
FileBase=$(basename $1)
FileExtension="${FileBase##*.}"
FileBase="${FileBase%.*}"
OutputSuffix="WF.$FileExtension"
OutputFile="$FileBase$OutputSuffix"
commandment="unix2dos -n $1 $OutputFile"
echo $commandment
unix2dos -n $1 $OutputFile
echo "diff -w $1 $OutputFile"
diff -w "$1" "$OutputFile"
exit
Example run
$ bd2w TestResults.txt
In u2w FileToConvert = TestResults.txt
unix2dos -n TestResults.txt TestResultsWF.txt
unix2dos: converting file TestResults.txt to file TestResultsWF.txt in DOS format ...
diff -w TestResults.txt TestResultsWF.txt

Wrap the script in a for file in "$#" loop and replace all mentions of $1 with $file.
#!/bin/sh
for file in "$#"; do
echo "In u2w FileToConvert = $file"
FileBase=$(basename "$file")
FileExtension="${FileBase##*.}"
FileBase="${FileBase%.*}"
OutputSuffix="WF.$FileExtension"
OutputFile="$FileBase$OutputSuffix"
echo "unix2dos -n $file $OutputFile"
unix2dos -n "$file" "$OutputFile"
echo "diff -w $file $OutputFile"
diff -w "$file" "$OutputFile"
done

Related

Shell Script to unzip files and read them sequentially

I have a list of zipped folders which contains xml files in it.
I want to write a linux shell script which unzips the folder sequentially , read them and print the file name if it contains particular string say "Apple INC."
I have tried following code.
for x in *.zip
do unzip $x
if grep -i "Apple INC" $x
then
echo $x
fi
done
can anyone help me in getting it right?
Thank you.
You can use unzip -p to unzip files to standard output, and pipe the output directly to grep. The -q options prevents grep from outputting the matching lines.
for z in *.zip ; do
if unzip -p "$z" | grep -qi 'apple inc' ; then
echo "$z"
fi
done
Have you noticed how the code is indented?

How to write shell script to create zip file for the files that had same string in file name

How to write simple shell script to create zip file.
I want to create zip file by collecting files with same string pattern in their file names from a folder.
For example, there may be many files under a folder.
xxxxx_20140502_xxx.txt
xxxxx_20140502_xxx.txt
xxxxx_20140503_xxx.txt
xxxxx_20140503_xxx.txt
xxxxx_20140504_xxx.txt
xxxxx_20140504_xxx.txt
After running the shell script, the result must be following three zip files.
20140502.zip
20140503.zip
20140504.zip
Please give me right direction to create simple shell script to output the result as above.
#!/bin/bash
for file in *_????????_*.csv *_????????_*.txt; do
[ -f "${file}" ] || continue
date=${file#*_} # adjust this and next line depending
date=${date%_*} # on your actual prefix/suffix
echo "${date}"
done | sort -u | while read date; do
zip "${date}.zip" *${date}*
done
Since zip will update the archive, this will do:
shopt -s nullglob
for file in *.{txt,csv}; do [[ $file =~ _([[:digit:]]{8})_ ]] && zip "${BASH_REMATCH[1]}.zip" "$file"; done
The shopt -s nullglob is because you don't want to have unexpanded globs if there are no matching files.
Everything below this line is my old answer...
First, get all the possible dates. Heuristically, this could be the files ending in .txt and .csv that match the regex _[[:digit:]]{8}_:
#!/bin/bash
shopt -s nullglob
declare -A dates=()
for file in *.{csv,txt}; do
[[ $file =~ _([[:digit:]]{8})_ ]] && dates[${BASH_REMATCH[1]}]=
done
printf "Date found: %s\n" "${!dates[#]}"
This will output to stdout all the dates found in the files. E.g. (I called the previous snipped gorilla and I chmod +x gorilla and touched a few files for demo):
$ ls
banana_20010101_gorilla.csv gorilla_20140502_bonobo.csv
gorilla notthisone_123_lol.txt
gorilla_20140502_banana.txt
$ ./gorilla
Date found: 20140502
Date found: 20010101
Next step, for each date found, get all the files ending in .txt and .csv and zip them in the archive corresponding to the date: appending this to gorilla will do the job:
for date in "${!dates[#]}"; do
zip "$date.zip" *"_${date}_"*.{csv,txt}
done
Full script after removing the flooding part:
#!/bin/bash
shopt -s nullglob
declare -A dates=()
for file in *.{csv,txt}; do
[[ $file =~ _([[:digit:]]{8})_ ]] && dates[${BASH_REMATCH[1]}]=
done
for date in "${!dates[#]}"; do
zip "$date.zip" *"_${date}_"*.{csv,txt}
done
Edit. I overlooked your requirement with one line command. Then here's the one-liner:
shopt -s nullglob; declare -A dates=(); for file in *.{csv,txt}; do [[ $file =~ _([[:digit:]]{8})_ ]] && dates[${BASH_REMATCH[1]}]=; done; for date in "${!dates[#]}"; do zip "$date.zip" *"_${date}_"*.{csv,txt}; done
:)
#! /bin/bash
dates=$(ls ?????_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_???.{csv,txt} \
| cut -f2 -d_ | sort -u)
for date in $dates ; do
zip $date.zip ?????_"$date"_???.{csv,txt}
done

Use Bash to Read File Then Do "grep" with The Line Against The File Itself

I am trying to read a file using Linux Bash and then use "grep" to run that line against the file itself. It seems not working to me...
#!/bin/bash
path=$1
while read line
do
var1=$(grep $line $path)
echo $?
exit
done < $path
The $? returns 1. What's going on here?
Use grep -F (fixed string) instead:
var1=$(grep -F "$line" "$path")

Renaming files in Shell Script Linux

I want to rename files I have downloaded from the following script:
exec < input_list.txt
while read line
do
get $line
wget ftp://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/$2/$4
# Rename $4
mv $4 $1"_"$3".bam"
done
The input file (input_list.txt) is tab delimited and contains four columns. The first, $1= name, $2= wget address, $3= factor and $4 is the file name.
A549 wgEncodeBroadHistone H2azDex100nm wgEncodeBroadHistoneA549H2azDex100nmAlnRep1.bam
I want to rename $4 (the file that has been downloaded) to a shorter file name that only includes the corresponding $1 and $3 terms. For example, wgEncodeBroadHistoneA549H2azDex100nmAlnRep1.bam
becomes A549_H2azDex100nm.bam
I've played around with " but I keep getting error messages for the mv command and that $4 is a bad variable name. Any suggestions would be greatly appreciated.
You don't need to rename the file if you use wget's -O option:
#!/bin/bash
[ -n "$BASH_VERSION" ] || {
echo "You need Bash to run this script."
exit 1
}
while IFS=$'\t' read -a INPUT; do
wget -O "${INPUT[0]}_${INPUT[2]}.bam" "ftp://hgdownload.cse.ucsc.edu/goldenPath/hg19/encodeDCC/${INPUT[1]}/${INPUT[3]}"
done < input_list.txt
Make sure you save the file in UNIX file format like script.sh and run bash script.sh.

Shell file parsing to automate youtube uploads

I was writing a small script to mass upload videos to youtube.. It looks like this:
#! /bin/sh
python --version
for file in ./*.mp4 ; do
export title=$(basename "$file" ".mp4")
echo $title "for" $file
youtube-upload -m mail#mailer.com -p pass -c Category -t "$title" -d "description" "$file"
done
Where youtube-upload is a cli based python uploader..
My question is as you can see i'm taking the title from the file name path, and the description is always the same :(. I want to write the description into a text or xml file parse it, and then upload with a proper description for each file..
How to do this using shell commands in this context?
Thanks for your suggestions
Have one file containing all of your filename->description mappings:
my file.mp4 this is a description
file2.mp4 this is another description
my last file.mp4 this is a description
ilied.mp4 description
And then just grab the line beginning with the filename and use the rest of the line:
#! /bin/sh
python --version
for file in ./*.mp4 ; do
export title=$(basename "$file" ".mp4")
echo $title "for" $file
youtube-upload -m mail#mailer.com -p pass -c Category -t "$title" -d "$(grep "^$(basename "$file")" desc | sed 's/.*.mp4 //')" "$file"
done
Looking at what's in the $(...):
grep "^$(basename "$file")" desc | sed 's/.*.mp4 //'
The grep finds the line that starts with the basename($file) in the "desc" file and then uses sed to get rid of the filename, leaving the rest of the line (which would be the description).
Note that this doesn't need to be a part of the youtube-upload comand line. You can also toss it into a variable:
#! /bin/sh
python --version
for file in ./*.mp4 ; do
export title=$(basename "$file" ".mp4")
echo $title "for" $file
description=$(grep "^$(basename "$file")" desc | sed 's/.*.mp4 //')
youtube-upload -m mail#mailer.com -p pass -c Category -t "$title" -d "$description" "$file"
done

Resources