Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX - Shell Scripting. Thank you! Step 1: Use grep/fgrep/egrep and sed to proce

ID: 3709816 • Letter: U

Question

UNIX - Shell Scripting. Thank you!

Step 1: Use grep/fgrep/egrep and sed to process individual files

The output of each file should be reduced to 19 rows and 2 fields separated by a single space character, as shown below:

F gge0001x

D 12-30-2006

T 14:15:20

S a69

B 15.8

M gge06001

P 30.1

Q 20.2

R 1006.2

U 1011.8

X 34.7

A 38.994

G 107.71

H 8.395

O 37.141

C 7.55

K 28.193

W 212.86

L 68.3

To do so:

Delete the unnecessary rows. Do not use line numbers, because your program can be tested on files where line numbers might not match

On each line, replace tab and multiple space characters with a single character

Extract the first field and save to a file called lineid

Extract the second field of each file and save as the curve name. For example, the second field of the first file should be saved into file gge0001x. A possible breakdown of this process is as follow:

Use the sed’s back reference feature to delete the first identification field

Since the second field is now the first field, use the back-reference feature to delete everything after the first field.

Save the output file as gge0001[xyz].

Explanation / Answer

CODE TO COPY :

#!/usr/bin/bash
# The following statement is used to check the output file already exist.
# If the output file exits then remove it.
if [ -s output.txt ]  
then  
rm output.txt
fi  

#The array values to append in front of every line                                                 
array=(F D T S B M P Q R U X A G H O C K W L)

i=0  
#Create a loop to read lines from input data file  
while read -r line  
do  
echo -n ${array[$i]} ' ' >> output.txt  
if [ $i -eq 3 ]
then  
echo "a69 a69 a69" >> output.txt  
elif [ $i -eq 5 ]
then  
echo "gge06001 gge06001 gge06001" >> output.txt
else  
echo "$line" | awk '{print $2 " " $3 " " $4 }' >> output.txt  
fi  
i=$((i+1))  
done < data.txt