2

I have an assignment saying "Using bash scripts and command line tools, implement the following functionality: Your created script should be run like this: and it should process *.txt files in that directory. This processing includes following steps: 1. make all alphabets small case 2. remove all xml/html tags 3. replace all acronyms found in text file 4. convert all numbers to words HINT:-You can use sed command."

enter image description here

I have written the code as shown in the image. I'm actually really new to linux so I had to search for different commands to make this work. However, as the assignment requires, the output should actually create another file, and write the modified content (after the applied 4 processes) on that new file, rather than printing every action on the terminal separately.

Code:

#1. Converts all text inside file 1.txt to lowercase
tr '[A-Z]' '[a-z]' < 1.txt
#2. Removes html/xml tags from the text
sed -e 's/<['^]>'*>//g' 1.txt
#3 Replaces all acronyms with full names
sed 's/doc/document/' 1.txt
#4 Converts all numbers to full words
sed 's/2/two/' 1.txt

File text: This is a RANDOM doc. There are 2 such docs. < b > I don't care < / b >

Expected output: this is a random document. there are two such documents. i don't care

Thanks!

1
  • 5
    Please don't post pictures of text. Post the text instead.
    – user147505
    Commented Oct 16, 2017 at 22:46

1 Answer 1

1

It looks like your problem is that none of your commands are modifying the file; they all write to standard output (i.e. the display the results in your terminal window). In general, two approaches come to mind:

  1. copy the file and use option flags which cause your commands to modify the copied file in-place, or

  2. use pipes and output redirection to apply all of the operations and write the result to a new file.

One problem with the first approach is that not every command-line program supports in-place file modification. In this case it just to happens that sed does have an option to edit the file in-place; using the -i option flag tells sed to edit the file in-place. The tr program does not support in-place editing, however you can easily replace your tr command with a sed command instead. This could lead us to something like the following (slightly modified) version of your script:

#0. Create a copy of the file
cp -i 1.txt 2.txt

#1. Converts all text inside file 2.txt to lowercase
sed -i -e 's/\(.*\)/\L\1/' 2.txt

#2. Removes html/xml tags from the text
sed -i -e 's/<['^]>'*>//g' 2.txt

#3 Replaces all acronyms with full names
sed -i -e 's/doc/document/' 2.txt

#4 Converts all numbers to full words
sed -i -e 's/2/two/' 2.txt

If we wanted to use pipes and redirection instead, we might end up with something like the following alternative:

cat 1.txt \
| sed -e 's/\(.*\)/\L\1/' \
| sed -e 's/<['^]>'*>//g' \
| sed -e 's/doc/document/' \
sed -e 's/2/two/' \
> 2.txt

Notice that this isn't a solution to your assignment; it only addresses your question regarding how to write changes to a file.

1
  • would you please explain how is 3rd command working. Where are the acronyms stored from where it is reading them
    – Abdullah
    Commented Oct 2, 2019 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .