LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-22-2016, 03:41 AM   #1
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 18

Rep: Reputation: Disabled
i am not able to concatenate both the string in shell script as below string


I have below file data as follows.

DSJOBSCHEDULE WWW_GFD_LTPER#XYZ_X0_0001_TEST
DESCRIPTION "Stream for source file validation; load; post load checks & archive for Source File"
N RUNCYCLE SCHEDID_1 "FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR,SA,SU"

DSJOBSCHEDULE WWW_GFD_LTPER#XYZ_X0_0002_TEST
DESCRIPTION "Stream for source file validation; load; post load checks & archive for Source File"
N RUNCYCLE SCHEDID_1 "FREQ=Daily;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR,SA,SU"

DSJOBSCHEDULE WWW_GFD_LTPER#XYZ_X0_0003_TEST
DESCRIPTION "Stream for source file validation; load; post load checks & archive for Source File"
N RUNCYCLE SCHEDID_1 "FREQ=Month;INTERVAL=1;BYDAY=MO"

need O/P as below:-

XYZ_X0_0001_TEST: FREQ=WEEKLY
XYZ_X0_0002_TEST: FREQ=Daily
XYZ_X0_0002_TEST: FREQ=Month

I tried as below:-
I am able to get the XYZ_X0_0001_TEST and FREQ=WEEKLY from file and i am not able to concatenate the above both the string in shell script.
cut -d'#' -f2 as.txt|grep XYZ --i will get XYZ_X0_0001_TEST and i am getting the grep -w SCHEDID_1 as.txt|cut -d' ' -f4|cut -d'"' -f2|cut -d';' -f1 = FREQ=WEEKLY
 
Old 04-22-2016, 03:59 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Code:
awk -F'[#";]' '/XYZ|FREQ/{printf $2 (/XYZ/?": ":"\n")}' file
 
2 members found this post helpful.
Old 04-22-2016, 04:09 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,879
Blog Entries: 1

Rep: Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998
I think the tool of choice here would be awk.

I was able to knock up a simple awk script that does the trick.

I put the awk in t.awk and pasted your data file into testfile.txt, then...

Code:
awk -f t.awk testfile.txt
XYZ_X0_0001_TEST: FREQ=WEEKLY
XYZ_X0_0002_TEST: FREQ=Daily
XYZ_X0_0003_TEST: FREQ=Month
I'll leave the details out so that you can enjoy the thrill of learning something new, but it really as simple as the following!

Code:
BEGIN{
        /* Define record separator and field separators here... */
}
{

        sub(/* Use regex to get the identifier from the first field here */);
        sub(/* Use regex to get the freq from the third field here */);
        print $1 ": " $3;
}
I bet someone can quickly reduce it to a oneliner!

** UPDATE: See what I mean! And grail types faster than I do as well!

Last edited by astrogeek; 04-22-2016 at 04:26 AM. Reason: Added space after colon, update
 
Old 04-22-2016, 05:39 AM   #4
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Code:
awk -F'[#";]' '/XYZ|FREQ/{printf $2 (/XYZ/?": ":"\n")}' file
thanks for your quick ans but i need to handle in loop condition like if while or for ...please help me to how to write the shell scripts..
 
Old 04-22-2016, 06:36 AM   #5
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 967
Blog Entries: 3

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
bash and the other shells can do while loops and other kinds of loops. What are you trying to use are criteria for looping and what have you gotten so far in your script?
 
Old 04-22-2016, 06:41 AM   #6
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
I tried in so many time

for i in grep -B4 SCHEDID_1 JD_file.txt
do
c=grep -B4 SCHEDID_1 JD_file.txt>a.txt
a=`grep SCHEDULE a.txt|cut -d'#' -f2`
b=`grep -w SCHEDID_1 a.txt`
echo "$a:$b"


while read line
do
echo $line
b=`grep -w SCHEDID_1 $line`
echo "$b"
a=`grep SCHEDULE JD_file.txt|cut -d'#' -f2`
echo "$a"
echo $a : $b
done < JD_file.txt ---
 
Old 04-22-2016, 07:02 AM   #7
Turbocapitalist
Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 967
Blog Entries: 3

Rep: Reputation: 395Reputation: 395Reputation: 395Reputation: 395
If you are just trying to read in one file a single time, then the grail's excellent awk solution above would work:

Code:
awk -F'[#";]' '/XYZ|FREQ/{printf $2 (/XYZ/?": ":"\n")}' JD_file.txt
If you want to monitor a single file in an ongoing manner, then you could pipe the output of tail into awk

Code:
tail -f JD_file.txt | awk -F'[#";]' '/XYZ|FREQ/{printf $2 (/XYZ/?": ":"\n")}'
By the way, with awk the one thing to know is that it is just a bunch of abbreviated if-then statements if nothing else is specified.
 
Old 04-22-2016, 07:12 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Not sure I understand what you mean about using a loop? Both your examples are bash type solutions to do exactly what the awk has done. You do realise that the awk will work on the entire file?

Or have we missed something in that you want it as a bash script instead of something else?
 
1 members found this post helpful.
Old 04-22-2016, 07:52 AM   #9
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
Hi Grail,

Very thanks for your Quick response.

in the bench file it is not working properly ..
 
Old 04-22-2016, 07:58 AM   #10
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
sorry it is working as expected i had taken wrong file...thanks grail and marked as Yes
 
Old 04-22-2016, 09:04 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Glad you got it sorted, don't forget to mark the question as SOLVED
 
Old 04-22-2016, 09:07 AM   #12
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
i already marked as solved...but we can do it in another way just asking?
 
Old 04-22-2016, 10:18 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
As per my signature, you need to go to the Thread Tools just above your first post and select it from the drop down
 
Old 04-22-2016, 01:32 PM   #14
spatil20
LQ Newbie
 
Registered: May 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
please let me know this example can be sloved in loop also?
 
Old 04-23-2016, 12:54 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Yes I was able to do this in a while loop in bash. Have a go and think along the same lines as the awk solution.
 
  


Reply

Tags
concatenate


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to find a string and print x lines before and y lines after the string igorza Linux - Newbie 6 04-18-2013 05:31 PM
How to concatenate two strings into one string in B-shell? jimmyjiang Red Hat 5 01-08-2008 02:15 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-21-2007 12:14 AM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 04:42 AM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration