Merge 2 Files and Sort with Headers into a 3rd - shell

I'm trying to take the 2 files below, and create a script to sort them into a new file of 3 column's with headers and some additional info.
I know the command to combine files and sort -- cat file1 file2 | sort > file3 but I don't know how to align the column's or add headings.
File 1
Dave 734.838.9800
Bob 313.123.4567
Carol 248.344.5576
Mary 313.449.1390
Ted 248-496-2204
Alice 616.556.4458
File 2
Bob Tuesday
Carol Monday
Ted Sunday
Alice Wednesday
Dave Thursday
Mary Saturday
Anticipated New File
Name On-Call Phone
Carol MONDAY 248.344.5576
Bob TUESDAY 313.123.4567
Alice WEDNESDAY 616.556.4458
Dave THURSDAY 734.838.9800
Mary SATURDAY 313.449.1390
Ted SUNDAY 248.496.2204

Related

Shell script awk concatenate text with entire column

I have a csv file Final.csv and i am trying to change its first coulmn and add a text it each column row
like this is the csv
Trialtime type Track time Notes Athlete
07:15:00 Warmup ABC 85.2 Initial warmup Jon
07:45:00 Sprint1 ABC 59.44 First Sprint Jon
08:30:00 TRIAL ABC 57.21 Final attempt Jon
08:00:00 Warmup ABC 120.51 Initial warmup Bill
08:40:05 Sprint1 ABC 61.35 First Sprint Bill
09:15:00 Sprint2 ABC 60.08 Second Sprint Bill
10:30:00 TRIAL ABC 60.37 Final attempt Bill
and i want to add a text to the trial time coulmn like this
Trialtime type Track time Notes Athlete
2012 07:15:00 Warmup ABC 85.2 Initial warmup Jon
2012 07:45:00 Sprint1 ABC 59.44 First Sprint Jon
2012 08:30:00 TRIAL ABC 57.21 Final attempt Jon
2012 08:00:00 Warmup ABC 120.51 Initial warmup Bill
2012 08:40:05 Sprint1 ABC 61.35 First Sprint Bill
2012 09:15:00 Sprint2 ABC 60.08 Second Sprint Bill
2012 10:30:00 TRIAL ABC 60.37 Final attempt Bill
i try to print each line row in that coulmn like this with awk
awk -F',' 'NR>1{print $1}1' OFS=',' $OutFileName
NR>1 is for not including the headers
but i want to add a text variable to each row in the coulmn
lets say
text="2012"
now i want to modify the trial time column rows with this 2012 appended to each row
what i wa doing was
awk -F',' 'NR>1{$1$text;}1' OFS=',' $OutFileName
but the text variable does not work inside awk what to do ?
and i also want to keep the headers.
awk -v txt="2012" 'NR==1||$0=txt FS $0' yourFile
For the input and output example, this will do what you want.
awk solution:
awk -v t="2012" 'BEGIN{ FS=OFS="\t" }NR>1{ $1=t" "$1 }1' file
The output:
Trialtime type Track time Notes Athlete
2012 07:15:00 Warmup ABC 85.2 Initial warmup Jon
2012 07:45:00 Sprint1 ABC 59.44 First Sprint Jon
2012 08:30:00 TRIAL ABC 57.21 Final attempt Jon
2012 08:00:00 Warmup ABC 120.51 Initial warmup Bill
2012 08:40:05 Sprint1 ABC 61.35 First Sprint Bill
2012 09:15:00 Sprint2 ABC 60.08 Second Sprint Bill
2012 10:30:00 TRIAL ABC 60.37 Final attempt Bill

filter file content to sorted tables

I have a file that contains the following lines of code. here the file displays a schedules which are sorted one by one .
at 12:00 the schedule of james version1 is :
first_task:eating:nothing
second_task:rest:onehour
third_task:watching:nothing
at 12:00 the schedule of james version2 is :
first_task:eating:fruits
second_task:rest:twohour
third_task:watching:manga
at 12:00 the schedule of alex version1 is :
first_task:eating:fruit
second_task:rest:halfhour
third_task:watching:horrorfilm
at 12:00 the schedule of alex version2 is :
first_task:eating:meal
second_task:rest:nothing
third_task:watching:nothing
at 18:00 the schedule of james version1 is :
first_task:eating:fastfood
second_task:rest:twohours
third_task:watching:series
at 18:00 the schedule of james version2 is :
first_task:eating:nothing
second_task:rest:onehours
third_task:watching:series
at 18:00 the schedule of alex version1 is :
first_task:eating:vegetals
second_task:rest:threehours
third_task:watching:manga
at 18:00 the schedule of alex version2 is :
first_task:eating:bread
second_task:rest:fivehours
third_task:watching:manga
at 22:00 the schedule of james version1 is :
first_task:eating:nothing
second_task:rest:sevenhours
third_task:watching:nothing
at 22:00 the schedule of james version2 is :
first_task:eating:meal
second_task:rest:sixnhours
third_task:watching:nothing
at 22:00 the schedule of alex version1 is :
first_task:eating:vegetals
second_task:rest:sevehours
third_task:watching:manga
at 22:00 the schedule of alex version2 is :
first_task:eating:icecream
second_task:rest:sevenhours
third_task:watching:nothing
I've tried to sort it this way :
12:00 eating:fruit
18:00 eating:vegetals
22:00 eating:nothing
12:00 rest:onhour
18:00 rest:threehour
22:00 rest:sevenhour
12:00 watching:horrorfilm
18:00 watching:manga
22:00 watching:nothing
using these commands :
awk -F '[\ :]' '/the schedule is/{h=$2;m=$3} /eating/{print " "h":"m" watching:"$3}' f.txt
awk -F '[\ :]' '/the schedule is/{h=$2;m=$3} /rest/{print " "h":"m" rest:"$3}' f.txt
awk -F '[\ :]' '/the schedule is/{h=$2;m=$3} /watching/{print " "h":"m" watching:"$3}' f.txt
Now I am looking to improve the filtered file by ignoring all non significant words and sorting the most valuable information in a table , I've tried to think/search how get this format but in vain .

Combine text from two files, output to another [duplicate]

This question already has answers here:
Inner join on two text files
(5 answers)
Closed 1 year ago.
i'm having a bit of a problem and i've been searching allll day. this is my first Unix class don't be to harsh.
so this may sound fairly simple, but i can't get it
I have two text files
file1
David 734.838.9801
Roberto‭ ‬313.123.4567
Sally‭ ‬248.344.5576
Mary‭ ‬313.449.1390
Ted‭ ‬248.496.2207
Alice‭ ‬616.556.4458
Frank‭ ‬634.296.1259
file2
Roberto Tuesday‭ ‬2
Sally Monday‭ ‬8
Ted Sunday‭ ‬16
Alice Wednesday‭ ‬23
David Thursday‭ ‬10
Mary Saturday‭ ‬14
Frank Friday‭ ‬15
I am trying to write a script using a looping structure that will combine both files and come out with the output below as a separate file
output:
Name On-Call Phone Start Time
Sally Monday 248.344.5576 8am
Roberto Tuesday 313.123.4567 2am
Alice‭ Wednesday‭ 616.556.4458‭ 11pm
David‭ Thursday‭ 734.838.9801‭ 10am
Frank‭ Friday‭ 634.296.1259‭ 3pm
Mary‭ Saturday‭ 313.449.1390‭ 2pm
Ted‭ ‬ Sunday‭ 248.496.2207‭ 4pm
This is what i tried( i know its horrible)
echo " Name On-Call Phone Start Time"
file="/home/xubuntu/date.txt"
file1="/home/xubuntu/name.txt"
while read name2 phone
do
while read name day time
do
echo "$name $day $phone $time"
done<"$file"
done<"$file1"
any help would be appreciated
First, sort the files using sort and then use this command:
paste file1 file2 | awk '{print $1,$4,$2,$5}'
This will bring you pretty close. After that you have to figure out how to format the time from the 24 hour format to the 12 hour format.
If you want to avoid using sort separately, you can bring in a little more complexity like this:
paste <(sort file1) <(sort file2) | awk '{print $1,$4,$2,$5}'
Finally, if you have not yet figured out how to print the time in 12 hour format, here is your full command:
paste <(sort file1) <(sort file2) | awk '{"date --date=\"" $5 ":00:00\" +%I%P" |& getline $5; print $1 " " $4 " " $2 " " $5 }'
You can use tabs (\t) in place of spaces as connectors to get a nicely formatted output.
In this case join command will also work,
join -1 1 -2 1 <(sort file1) <(sort file2)
Description
-1 -> file1
1 -> first field of file1 (common field)
-2 -> file2
1 -> first field of file2 (common field)
**cat file1**
David 734.838.9801
Roberto 313.123.4567
Sally 248.344.5576
Mary 313.449.1390
Ted 248.496.2207
Alice 616.556.4458
Frank 634.296.1259
**cat file2**
Roberto Tuesday 2
Sally Monday 8
Ted Sunday 16
Alice Wednesday 23
David Thursday 10
Mary Saturday 14
Frank Friday 15
output
Alice 616.556.4458 Wednesday 23
David 734.838.9801 Thursday 10
Frank 634.296.1259 Friday 15
Mary 313.449.1390 Saturday 14
Roberto 313.123.4567 Tuesday 2
Sally 248.344.5576 Monday 8
Ted 248.496.2207 Sunday 16

Convert multi-line file into TSV using awk

Am using Windows 7 & gawk 3.1.3 (via UnxUtils).
I'd like to turn this input (Liverpool FC's fixtures):
Sunday, 27 November 2011
Barclays Premier League
Liverpool v Man City, 16:00
Tuesday, 29 November 2011
Carling Cup
Chelsea v Liverpool, QF, 19:45
...
into a tab-separated file, such as:
Sunday, 27 November 2011<tab>Barclays Premier League<tab>Liverpool v Man City, 16:00
Tuesday, 29 November 2011<tab>Carling Cup<tab>Chelsea v Liverpool, QF, 19:45
...
I've tried doing this with awk, but failed thus far. Identifying every first and second line is easy enough:
if (NR % 3 == 1 || NR % 3 == 2) print;
but despite many attempts (usually resulting in syntax errors) can't find out how to strip out the (Windows) line-endings and concatenate those with every third line.
I'm now wondering if awk is actually the right tool for the job.
Thanks for any pointers.
awk '(NR % 3) > 0 {printf("%s\t",$0)}
(Nr % 3) == 0 {printf("%s\n",$0)}
Should work. For every line where the modulo of NR (number of records) is not 0 it will print the line and a tab character. Otherwise the (input) line and a newline character.
HTH
see the test below:
kent$ echo "Sunday, 27 November 2011
Barclays Premier League
Liverpool v Man City, 16:00
Tuesday, 29 November 2011
Carling Cup
Chelsea v Liverpool, QF, 19:45
"|awk '{printf $0"\t";if(!(NR%3))print""}'
output:
Sunday, 27 November 2011 Barclays Premier League Liverpool v Man City, 16:00
Tuesday, 29 November 2011 Carling Cup Chelsea v Liverpool, QF, 19:45

How in Bash script receive Tuesday before last Tuesday?

I know that last Tuesday:
date --date="2 Tuesday"
But if I need to get Tuesday before Tuesday ? -2 doesn't work.
Thanks.
date --date='Tuesday - 2 weeks'
If it's tuesday it is
date --date='Today - 2 weeks'

Resources