Extracting text from a file when I know its format

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am running a script which will output a file called scServer.scs which looks something like this:




49138@bber0501u29b.bb2.cluster



2



Solution 1



Fluid Flow (Fluent)



Solution



Transient Structural



7443_bber0501u29b.bb2.cluster



27412@lic-server1.abc.ac.uk




Later in the script I need to reference the "49138" and "bber0501u29b.bb2.cluster" but they will be different every time I run the script. So after my script generates scServer.scs I need to extract those pieces of text and write them as variables?










share|improve this question



















  • 1





    is the data always on line 1? or the first one in the format of user@host? How do we know it's the right data?

    – Jeff Schaller
    Mar 7 at 15:58











  • It is always in that format, always on line 1, I belive RoVo has the answer

    – CFD-Tester
    Mar 7 at 16:17

















1















I am running a script which will output a file called scServer.scs which looks something like this:




49138@bber0501u29b.bb2.cluster



2



Solution 1



Fluid Flow (Fluent)



Solution



Transient Structural



7443_bber0501u29b.bb2.cluster



27412@lic-server1.abc.ac.uk




Later in the script I need to reference the "49138" and "bber0501u29b.bb2.cluster" but they will be different every time I run the script. So after my script generates scServer.scs I need to extract those pieces of text and write them as variables?










share|improve this question



















  • 1





    is the data always on line 1? or the first one in the format of user@host? How do we know it's the right data?

    – Jeff Schaller
    Mar 7 at 15:58











  • It is always in that format, always on line 1, I belive RoVo has the answer

    – CFD-Tester
    Mar 7 at 16:17













1












1








1








I am running a script which will output a file called scServer.scs which looks something like this:




49138@bber0501u29b.bb2.cluster



2



Solution 1



Fluid Flow (Fluent)



Solution



Transient Structural



7443_bber0501u29b.bb2.cluster



27412@lic-server1.abc.ac.uk




Later in the script I need to reference the "49138" and "bber0501u29b.bb2.cluster" but they will be different every time I run the script. So after my script generates scServer.scs I need to extract those pieces of text and write them as variables?










share|improve this question
















I am running a script which will output a file called scServer.scs which looks something like this:




49138@bber0501u29b.bb2.cluster



2



Solution 1



Fluid Flow (Fluent)



Solution



Transient Structural



7443_bber0501u29b.bb2.cluster



27412@lic-server1.abc.ac.uk




Later in the script I need to reference the "49138" and "bber0501u29b.bb2.cluster" but they will be different every time I run the script. So after my script generates scServer.scs I need to extract those pieces of text and write them as variables?







linux text-processing awk sed scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 15:57









Jeff Schaller

44.6k1162145




44.6k1162145










asked Mar 7 at 15:43









CFD-TesterCFD-Tester

103




103







  • 1





    is the data always on line 1? or the first one in the format of user@host? How do we know it's the right data?

    – Jeff Schaller
    Mar 7 at 15:58











  • It is always in that format, always on line 1, I belive RoVo has the answer

    – CFD-Tester
    Mar 7 at 16:17












  • 1





    is the data always on line 1? or the first one in the format of user@host? How do we know it's the right data?

    – Jeff Schaller
    Mar 7 at 15:58











  • It is always in that format, always on line 1, I belive RoVo has the answer

    – CFD-Tester
    Mar 7 at 16:17







1




1





is the data always on line 1? or the first one in the format of user@host? How do we know it's the right data?

– Jeff Schaller
Mar 7 at 15:58





is the data always on line 1? or the first one in the format of user@host? How do we know it's the right data?

– Jeff Schaller
Mar 7 at 15:58













It is always in that format, always on line 1, I belive RoVo has the answer

– CFD-Tester
Mar 7 at 16:17





It is always in that format, always on line 1, I belive RoVo has the answer

– CFD-Tester
Mar 7 at 16:17










3 Answers
3






active

oldest

votes


















3














If your data is always in the first line and in format user@domain, you can do this:



Extract the first line and use shell parameter expansion:



firstline=$(head -n1 file)
user=$firstline%@*
domain=$firstline##*@





share|improve this answer






























    0














    Using read and awk:



    read -r user domain < <(awk -F@ 'NR==1print $1,$2' scServer.scs)


    Will extract the first line of scServer.scs and separate two fields using @ as the field separator, assign the first field to the user variable and the second to the domain variable






    share|improve this answer






























      0














      g=`awk -F "@" 'NR==1print $1' filename `
      y=`awk -F "@" 'NR==1print $NF' filename `


      praveen@praveen:/tmp$ echo $g
      49138
      praveen@praveen:/tmp$ echo $y
      bber0501u29b.bb2.cluster





      share|improve this answer























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504948%2fextracting-text-from-a-file-when-i-know-its-format%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        If your data is always in the first line and in format user@domain, you can do this:



        Extract the first line and use shell parameter expansion:



        firstline=$(head -n1 file)
        user=$firstline%@*
        domain=$firstline##*@





        share|improve this answer



























          3














          If your data is always in the first line and in format user@domain, you can do this:



          Extract the first line and use shell parameter expansion:



          firstline=$(head -n1 file)
          user=$firstline%@*
          domain=$firstline##*@





          share|improve this answer

























            3












            3








            3







            If your data is always in the first line and in format user@domain, you can do this:



            Extract the first line and use shell parameter expansion:



            firstline=$(head -n1 file)
            user=$firstline%@*
            domain=$firstline##*@





            share|improve this answer













            If your data is always in the first line and in format user@domain, you can do this:



            Extract the first line and use shell parameter expansion:



            firstline=$(head -n1 file)
            user=$firstline%@*
            domain=$firstline##*@






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 7 at 15:58









            RoVoRoVo

            3,442317




            3,442317























                0














                Using read and awk:



                read -r user domain < <(awk -F@ 'NR==1print $1,$2' scServer.scs)


                Will extract the first line of scServer.scs and separate two fields using @ as the field separator, assign the first field to the user variable and the second to the domain variable






                share|improve this answer



























                  0














                  Using read and awk:



                  read -r user domain < <(awk -F@ 'NR==1print $1,$2' scServer.scs)


                  Will extract the first line of scServer.scs and separate two fields using @ as the field separator, assign the first field to the user variable and the second to the domain variable






                  share|improve this answer

























                    0












                    0








                    0







                    Using read and awk:



                    read -r user domain < <(awk -F@ 'NR==1print $1,$2' scServer.scs)


                    Will extract the first line of scServer.scs and separate two fields using @ as the field separator, assign the first field to the user variable and the second to the domain variable






                    share|improve this answer













                    Using read and awk:



                    read -r user domain < <(awk -F@ 'NR==1print $1,$2' scServer.scs)


                    Will extract the first line of scServer.scs and separate two fields using @ as the field separator, assign the first field to the user variable and the second to the domain variable







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 7 at 16:55









                    Jesse_bJesse_b

                    14.3k33574




                    14.3k33574





















                        0














                        g=`awk -F "@" 'NR==1print $1' filename `
                        y=`awk -F "@" 'NR==1print $NF' filename `


                        praveen@praveen:/tmp$ echo $g
                        49138
                        praveen@praveen:/tmp$ echo $y
                        bber0501u29b.bb2.cluster





                        share|improve this answer



























                          0














                          g=`awk -F "@" 'NR==1print $1' filename `
                          y=`awk -F "@" 'NR==1print $NF' filename `


                          praveen@praveen:/tmp$ echo $g
                          49138
                          praveen@praveen:/tmp$ echo $y
                          bber0501u29b.bb2.cluster





                          share|improve this answer

























                            0












                            0








                            0







                            g=`awk -F "@" 'NR==1print $1' filename `
                            y=`awk -F "@" 'NR==1print $NF' filename `


                            praveen@praveen:/tmp$ echo $g
                            49138
                            praveen@praveen:/tmp$ echo $y
                            bber0501u29b.bb2.cluster





                            share|improve this answer













                            g=`awk -F "@" 'NR==1print $1' filename `
                            y=`awk -F "@" 'NR==1print $NF' filename `


                            praveen@praveen:/tmp$ echo $g
                            49138
                            praveen@praveen:/tmp$ echo $y
                            bber0501u29b.bb2.cluster






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 10 at 13:20









                            Praveen Kumar BSPraveen Kumar BS

                            1,7391311




                            1,7391311



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504948%2fextracting-text-from-a-file-when-i-know-its-format%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown






                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay