How to remove part of the line from start of the line?

Hello,

I am java command from a shell script which will generate the below output on the command prompt

 
 signature Base64 : OS8nJnqmzM5GLu0L8VHuKj6ZucZ1KGJXEhxzA9xk+4EUyFKYTkEMMGB93ybUsMP0f4nPp6MDF7wL8i4mHN9HGN58br6yEXNQk6+pUk5nM2Mids2xOyJqBjOWn2QBI7MHfiCxMRENAJXa3vUqZIQkRJWQeGOdmnYecGyf0HDuXTRgUMECSqoAhrsjWxg+ENJTyQlv79u7H9OrDB6jXCcekw9GNFslIULE5Oe1Jid4CV7YVwS4VCD5Ze000dVbFC0iqk7JQcAaEY+3CeQCn36aGpYj+TFFfpz/sLV7+PQnd8RrVK/1Si69OCrEe6+3xhb5Solxs8A4INmJb0YMfBIR8w==
 signature hex    : 392F27267AA6CCCE462EED0BF151EE2A3E99B9C675286257121C7303DC64FB8114C852984E410C30607DDF26D4B0C3F47F89CFA7A30317BC0BF22E261CDF4718DE7C6EBEB211735093AFA9524E6733632276CDB13B226A0633969F640123B3077E20B131110D0095DADEF52A64842444959078639D9A761E706C9FD070EE5D346050C1024AAA0086BB235B183E10D253C9096FEFDBBB1FD3AB0C1EA35C271E930F46345B252142C4E4E7B5262778095ED85704B85420F965ED34D1D55B142D22AA4EC941C01A118FB709E4029F7E9A1A9623F931457E9CFFB0B57BF8F42777C46B54AFF54A2EBD382AC47BAFB7C616F94A8971B3C03820D9896F460C7C1211F3
  
 

My requirement is to only pick the first line after �signature Base64 :� i.e. only Base64 signature. Now I can pull first line of the file using

sed -n �1P' filename 

but I do not know how to remove �signature Based64 :� (and the whitespace) from the first line? All I want is the blue highlighted part of the output.

Also is there any way I can get this line(highlighted in blue above) in a variable so that I can reuse it further down the script?

Hello chetanojha,

Could you please try following and let me know if this helps you.

awk '/signature Base64/{print $NF}'   Input_file

Thanks,
R. Singh

cat testfile
signature Base64 : OS8nJnqmzM5GLu0L8VHuKj6ZucZ1KGJXEhxzA9xk+4EUyFKYTkEMMGB93ybUsMP0f4nPp6MDF7wL8i4mHN9HGN58br6yEXNQk6+pUk5nM2Mids2xOyJqBjOWn2QBI7MHfiCxMRENAJXa3vUqZIQkRJWQeGOdmnYecGyf0HDuXTRgUMECSqoAhrsjWxg+ENJTyQlv79u7H9OrDB6jXCcekw9GNFslIULE5Oe1Jid4CV7YVwS4VCD5Ze000dVbFC0iqk7JQcAaEY+3CeQCn36aGpYj+TFFfpz/sLV7+PQnd8RrVK/1Si69OCrEe6+3xhb5Solxs8A4INmJb0YMfBIR8w==
 signature hex    : 392F27267AA6CCCE462EED0BF151EE2A3E99B9C675286257121C7303DC64FB8114C852984E410C30607DDF26D4B0C3F47F89CFA7A30317BC0BF22E261CDF4718DE7C6EBEB211735093AFA9524E6733632276CDB13B226A0633969F640123B3077E20B131110D0095DADEF52A64842444959078639D9A761E706C9FD070EE5D346050C1024AAA0086BB235B183E10D253C9096FEFDBBB1FD3AB0C1EA35C271E930F46345B252142C4E4E7B5262778095ED85704B85420F965ED34D1D55B142D22AA4EC941C01A118FB709E4029F7E9A1A9623F931457E9CFFB0B57BF8F42777C46B54AFF54A2EBD382AC47BAFB7C616F94A8971B3C03820D9896F460C7C1211F3
cat testfile | awk -F ":" 'NR==1 {print $2}' | sed 's/^ //'

Output:

OS8nJnqmzM5GLu0L8VHuKj6ZucZ1KGJXEhxzA9xk+4EUyFKYTkEMMGB93ybUsMP0f4nPp6MDF7wL8i4mHN9HGN58br6yEXNQk6+pUk5nM2Mids2xOyJqBjOWn2QBI7MHfiCxMRENAJXa3vUqZIQkRJWQeGOdmnYecGyf0HDuXTRgUMECSqoAhrsjWxg+ENJTyQlv79u7H9OrDB6jXCcekw9GNFslIULE5Oe1Jid4CV7YVwS4VCD5Ze000dVbFC0iqk7JQcAaEY+3CeQCn36aGpYj+TFFfpz/sLV7+PQnd8RrVK/1Si69OCrEe6+3xhb5Solxs8A4INmJb0YMfBIR8w==

I think this will help you.

Thanks.

You could also try one of these:-

sed -n �1P' filename | read drop1 drop2 drop3 value
echo $value
while read drop1 drop2 drop3
do
   echo $value
done < <(sed -n �1P' filename)
value=$(sed -n �1P' filename)
value=${value#*: }
echo $value

Do any of these help?

Robin

Read the first line and disregard what you do not want.

read _ _ _ sig < chetanojha.example

Now you have a variable named sig

$ echo $sig
OS8nJnqmzM5GLu0L8VHuKj6ZucZ1KGJXEhxzA9xk+4EUyFKYTkEMMGB93ybUsMP0f4nPp6MDF7wL8i4mHN9HGN58br6yEXNQk6+pUk5nM2Mids2xOyJqBjOWn2QBI7MHfiCxMRENAJXa3vUqZIQkRJWQeGOdmnYecGyf0HDuXTRgUMECSqoAhrsjWxg+ENJTyQlv79u7H9OrDB6jXCcekw9GNFslIULE5Oe1Jid4CV7YVwS4VCD5Ze000dVbFC0iqk7JQcAaEY+3CeQCn36aGpYj+TFFfpz/sLV7+PQnd8RrVK/1Si69OCrEe6+3xhb5Solxs8A4INmJb0YMfBIR8w==

---------- Post updated at 07:15 AM ---------- Previous update was at 06:59 AM ----------

Hi Sumanthsv,

While your suggestion will produce acceptable result, it has unnecessary steps.
The use of cat is superfluous. AWK and sed can read from a file without the help of cat .
i.e.

awk -F ":" 'NR==1 {print $2}' testfile | sed 's/^ /

But also, AWK doesn't need the help of sed .
i.e.

 awk -F ":" 'NR==1 {sub("^ ", "", $2); print $2}' testfile

Pipes are great but not necessary here.

Still, AWK can do the job without that extra work of removing the blank in front, if we do:

awk 'NR==1 {print $4}' testfile

Keep up the good work!

sed -n '/signature Base64/ s/.*: //p;Q'

will search for the first occurrence of signature Base64 ; deleted everything before the colon-space; print; exit.
Subsequent occurrences of signature Base64 will be ignored.

BUG: if the signature contains colon-space it will be cut. But that shouldn't happen for the data you are using it with.

Andrew

PS I should mentions that the Q in the sed command is a GNU extension and will not work with Unix sed.

Dear All,

Yes this works fine. AWK command has done the job for me. I need to know now is how can i add this output to a file.

I ran below which brings the required result in the variable.

cert1=`awk -F ":" 'NR==1 {sub("^ ", "", $2); print $2}' $temp_file`

when i am adding this output at the end of the file, it is adding ^M character in the whole file. I am using below

echo ${cert1} >> main_file.txt 

One another thought, is there any way I can run java command as shown below and awk the output directly putting it into a file. Which means output of the java command should feed to the awk (which will pull blue line above) and add it to the third file?

Hello chetanojha,

Could you please try following to remove control M characters and let me know if this helps you.

tr -d '\r' < Input_file
OR
awk '{gsub(/\r/,"");print}' Input_file

Thanks,
R. Singh

Hi Ravinder,

Both worked well on command line. But i am trying to write the content back to file where I do not see any chances.

I am trying below.

 awk '{gsub(/\r/,"");print}' Input_file  >>  Input_file 

Hello chetanojha,

Command you ran is again putting the output into same Input_file, note that awk doesn't have that facility in it, you could further try following on same then.

awk '{gsub(/\r/,"");print}' Input_file > temp_file && mv temp_file Input_file

So above will take output to a temp_file and then it will rename it to Input_file, let me know if this helps you.

Thanks,
R. Singh

1 Like

Actually, you can do this in awk without a temporary file because this is a special case:

awk '{sub(/\r/,""); print $NF > FILENAME; exit}' Input_file

The special case works only when awk has already read everything it needs from your input file before it attempts to write anything back to your input file and quits before trying to read anything else.

1 Like