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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove part of the line from start of the line?
# 1  
Old 05-23-2017
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

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

Code:
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?
# 2  
Old 05-23-2017
Hello chetanojha,

Could you please try following and let me know if this helps you.
Code:
awk '/signature Base64/{print $NF}'   Input_file

Thanks,
R. Singh
# 3  
Old 05-23-2017
Code:
cat testfile
signature Base64 : OS8nJnqmzM5GLu0L8VHuKj6ZucZ1KGJXEhxzA9xk+4EUyFKYTkEMMGB93ybUsMP0f4nPp6MDF7wL8i4mHN9HGN58br6yEXNQk6+pUk5nM2Mids2xOyJqBjOWn2QBI7MHfiCxMRENAJXa3vUqZIQkRJWQeGOdmnYecGyf0HDuXTRgUMECSqoAhrsjWxg+ENJTyQlv79u7H9OrDB6jXCcekw9GNFslIULE5Oe1Jid4CV7YVwS4VCD5Ze000dVbFC0iqk7JQcAaEY+3CeQCn36aGpYj+TFFfpz/sLV7+PQnd8RrVK/1Si69OCrEe6+3xhb5Solxs8A4INmJb0YMfBIR8w==
 signature hex    : 392F27267AA6CCCE462EED0BF151EE2A3E99B9C675286257121C7303DC64FB8114C852984E410C30607DDF26D4B0C3F47F89CFA7A30317BC0BF22E261CDF4718DE7C6EBEB211735093AFA9524E6733632276CDB13B226A0633969F640123B3077E20B131110D0095DADEF52A64842444959078639D9A761E706C9FD070EE5D346050C1024AAA0086BB235B183E10D253C9096FEFDBBB1FD3AB0C1EA35C271E930F46345B252142C4E4E7B5262778095ED85704B85420F965ED34D1D55B142D22AA4EC941C01A118FB709E4029F7E9A1A9623F931457E9CFFB0B57BF8F42777C46B54AFF54A2EBD382AC47BAFB7C616F94A8971B3C03820D9896F460C7C1211F3

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

Output:

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

I think this will help you.

Thanks.
# 4  
Old 05-23-2017
You could also try one of these:-
Code:
sed -n ‘1P' filename | read drop1 drop2 drop3 value
echo $value

Code:
while read drop1 drop2 drop3
do
   echo $value
done < <(sed -n ‘1P' filename)

Code:
value=$(sed -n ‘1P' filename)
value=${value#*: }
echo $value

Do any of these help?



Robin
# 5  
Old 05-23-2017
Read the first line and disregard what you do not want.
Code:
read _ _ _ sig < chetanojha.example

Now you have a variable named sig
Code:
$ 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 ----------

Quote:
Originally Posted by Sumanthsv
Code:
cat testfile
signature Base64 : OS8nJnqmzM5GLu0L8VHuKj6ZucZ1KGJXEhxzA9xk+4EUyFKYTkEMMGB93ybUsMP0f4nPp6MDF7wL8i4mHN9HGN58br6yEXNQk6+pUk5nM2Mids2xOyJqBjOWn2QBI7MHfiCxMRENAJXa3vUqZIQkRJWQeGOdmnYecGyf0HDuXTRgUMECSqoAhrsjWxg+ENJTyQlv79u7H9OrDB6jXCcekw9GNFslIULE5Oe1Jid4CV7YVwS4VCD5Ze000dVbFC0iqk7JQcAaEY+3CeQCn36aGpYj+TFFfpz/sLV7+PQnd8RrVK/1Si69OCrEe6+3xhb5Solxs8A4INmJb0YMfBIR8w==
 signature hex    : 392F27267AA6CCCE462EED0BF151EE2A3E99B9C675286257121C7303DC64FB8114C852984E410C30607DDF26D4B0C3F47F89CFA7A30317BC0BF22E261CDF4718DE7C6EBEB211735093AFA9524E6733632276CDB13B226A0633969F640123B3077E20B131110D0095DADEF52A64842444959078639D9A761E706C9FD070EE5D346050C1024AAA0086BB235B183E10D253C9096FEFDBBB1FD3AB0C1EA35C271E930F46345B252142C4E4E7B5262778095ED85704B85420F965ED34D1D55B142D22AA4EC941C01A118FB709E4029F7E9A1A9623F931457E9CFFB0B57BF8F42777C46B54AFF54A2EBD382AC47BAFB7C616F94A8971B3C03820D9896F460C7C1211F3

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

Output:

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

I think this will help you.

Thanks.
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.
Code:
awk -F ":" 'NR==1 {print $2}' testfile | sed 's/^ /

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

Code:
 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:
Code:
awk 'NR==1 {print $4}' testfile

Keep up the good work!

Last edited by Aia; 05-23-2017 at 11:00 AM..
# 6  
Old 05-23-2017
Code:
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.

Last edited by apmcd47; 05-23-2017 at 11:04 AM.. Reason: Forgot to say the Q command is an extension to sed
# 7  
Old 05-23-2017
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.
Code:
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
Code:
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

4. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

5. Shell Programming and Scripting

Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format: I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one. Any help would be greatly appreciated. (10 Replies)
Discussion started by: nwalsh88
10 Replies

6. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

7. Shell Programming and Scripting

Using Sed to remove part of line with regex

Greetings everyone. Right now I am working on a script to be used during automated deployment of servers. What I have to do is remove localhost.localdomain and localhost6.localdomain6 from the /etc/hosts file. Simple, right? Except most of the examples I've found using sed want to delete the entire... (4 Replies)
Discussion started by: msarro
4 Replies

8. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies

9. Shell Programming and Scripting

remove part of a line

Hi I need some help with using shell script to analyze the content of a file. I hope someone can help me. I have a file with content like the following: /foldera/database/procedure/a.proc$$/version1/2 /folderb/database/procedure/proj1/b.proc$$/version2/2 I need to write a shell script to... (16 Replies)
Discussion started by: tiger99
16 Replies
Login or Register to Ask a Question