Write a bash script named stripTabs. The first argument to the script should be
ID: 3606627 • Letter: W
Question
Write a bash script named stripTabs. The first argument to the script should be the replacement string
for any tab found in a file. The remaining arguments, if any, are files. The script should loop over the
files passed as arguments, and for each file, replace every tab character with the desired replacement string.
If the file does not contain any tabs, then the file should not be changed; otherwise, a message should be
displayed, indicating that the tabs were removed from the file. For example:
[user]$ cat foo.txt bar.txt
foo has no tabs
bar has many tabs
[user]$ stripTabs "- T >" foo.txt bar.txt
Removed tabs from bar.txt
[user]$ cat foo.txt bar.txt
foo has no tabs
bar- T >has- T >- T >many- T >- T >- T >tabs
[user]$
You may assume that the replacement string does not contain any tab characters. Your script may invoke
sed and awk if needed
Explanation / Answer
CODE
#!/bin/bash
# Relacing Tab character with some name in a File
i=1;
renameText="$1"
for value in $@ #loop all the command line arguments
do
if [ $i == 1 ];then #skip first argument because its a rename Text
((i++))
continue
fi
if [ -e $value ];then #check file exist or not
if grep -q "$value"; then #checking if file contains Tab
sed -i -e 's/[ ]/'$renameText'/g' $value #Relacing Tab character
echo "removed Tabs in $value"
fi
else
echo "$value File Not Exist" #file not Exist
fi
done
IF ANY QUERIES REGARDING CODE PLEASE GET BACK TO ME
THANK YOU
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.