Write a program in either Ruby, Smalltalk, or BASH using www.compileonline.com U
ID: 3557209 • Letter: W
Question
Write a program in either Ruby, Smalltalk, or BASH using www.compileonline.com
Use the input.txt file to store names like:
Greed, Greg
Smith, Sally
Jones, James
Abernathy, Samantha
Have it read in the number of names to work on. Then read in each name, find the comma, and reverse the first name and last. Print the number and the new name, like so:
Name #1 Greg Greed
Name #2 Sally Smith
Name #3 James Jones
Name #4 Samantha Abernathy
below is what I have....but it doesn't reverse the first and last name
# bash script
#! /bin/bash
INPUT_FILE=input.txt
LINE_NO=1
while read line
do
set `echo $line |awk -F, '{print $1,$2}'`
VAL1=$1
VAL2=$2
echo "Name #$LINE_NO $VAL2 $VAL1"
let LINE_NO=LINE_NO+1
done < $INPUT_FILE
exit 0
Explanation / Answer
# bash script
#! /bin/bash
INPUT_FILE=input.txt
LINE_NO=1
while read line || [[ -n "$line" ]]; do
set `echo $line |awk -F, '{print $1,$2}'`
VAL1=$1
VAL2=$2
echo "Name #$LINE_NO $VAL2 $VAL1"
let LINE_NO=LINE_NO+1
done < $INPUT_FILE
NOTE: your programe is also correct.you just need to take care of the last element because there is no new line at the end of it .while alone is not able to read that line..
thankyou
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.