LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   i am not able to concatenate both the string in shell script as below string (https://www.linuxquestions.org/questions/linux-newbie-8/i-am-not-able-to-concatenate-both-the-string-in-shell-script-as-below-string-4175578079/)

spatil20 04-22-2016 02:41 AM

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

grail 04-22-2016 02:59 AM

Code:

awk -F'[#";]' '/XYZ|FREQ/{printf $2 (/XYZ/?": ":"\n")}' file

astrogeek 04-22-2016 03:09 AM

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!

spatil20 04-22-2016 04:39 AM

Quote:

Originally Posted by grail (Post 5534903)
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..

Turbocapitalist 04-22-2016 05:36 AM

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?

spatil20 04-22-2016 05:41 AM

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 ---

Turbocapitalist 04-22-2016 06:02 AM

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.

grail 04-22-2016 06:12 AM

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?

spatil20 04-22-2016 06:52 AM

Hi Grail,

Very thanks for your Quick response.

in the bench file it is not working properly ..

spatil20 04-22-2016 06:58 AM

sorry it is working as expected i had taken wrong file...thanks grail and marked as Yes

grail 04-22-2016 08:04 AM

Glad you got it sorted, don't forget to mark the question as SOLVED :)

spatil20 04-22-2016 08:07 AM

i already marked as solved...but we can do it in another way just asking?

grail 04-22-2016 09:18 AM

As per my signature, you need to go to the Thread Tools just above your first post and select it from the drop down :)

spatil20 04-22-2016 12:32 PM

please let me know this example can be sloved in loop also?

grail 04-22-2016 11:54 PM

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.


All times are GMT -5. The time now is 07:47 AM.