How to remove after nth space in each line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove after nth space in each line?
# 1  
Old 09-02-2018
How to remove after nth space in each line?

Hello,
My aim is:

* to remove anything after second space including second space itself
* or printing the first two fields

Concerning the second method, when I run something like
Code:
while read -r col1

, it prints the entire line in each row.

Myfile:
Code:
11.22.33.44 5566 ns*****.ip-*****.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no
22.33.44.55 6677 ns****.ip-****.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no
33.44.55.66 7788 ***.ip.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no-cac$
44.55.66.77 8899 ns***.ip-******.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no-ca


Expected output:
Code:
11.22.33.44 5566
22.33.44.55 6677
33.44.55.66 7788
44.55.66.77 8899

I tried below awk command
Code:
awk '{print substr($1,$2)}' Myfile

Not giving sought output.

I am able to skip the first two fields and print the rest with below command but can't do the reverse thing.

Code:
awk '{ $1=""; $2=""; print}' Myfile

Could you please let me know how to do that?

Thanks in advance
Boris
# 2  
Old 09-02-2018
Quote:
Originally Posted by baris35
Hello,
My aim is:
* to remove anything after second space including second space itself
* or printing the first two fields
Concerning the second method, when I run something like
Code:
while read -r col1

, it prints the entire line in each row.
Myfile:
Code:
11.22.33.44 5566 ns*****.ip-*****.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no
22.33.44.55 6677 ns****.ip-****.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no
33.44.55.66 7788 ***.ip.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no-cac$
44.55.66.77 8899 ns***.ip-******.net HTTP/1.1 401 Unauthorized\r\nServer: nCache-Control: no-ca


Expected output:
Code:
11.22.33.44 5566
22.33.44.55 6677
33.44.55.66 7788
44.55.66.77 8899

I tried below awk command
Code:
awk '{print substr($1,$2)}' Myfile

Not giving sought output.
I am able to skip the first two fields and print the rest with below command but can't do the reverse thing.
Code:
awk '{ $1=""; $2=""; print}' Myfile

Could you please let me know how to do that?
Thanks in advance
Boris
Hello baris35,

Could you please try following and let me know if this helps?
Code:
awk '{print $1,$2}' Input_file

substr is for getting sub string values related to indexes of the letters eg--> substr($0,1,10) which means get a sub string from current line starting from 1st character to next 10 characters.

Thanks,
R. Singh
# 3  
Old 09-02-2018
Hello,
Have just found a weird way
Just replaced all spaces by an underscore.
Code:
sed -i 's/ /_/g' Myfile

Then removed anything after second underscore
Code:
awk -F_ 'BEGIN {OFS="_"} // {print $1,$2} ! // {print}' Myfile

Many thanks
Boris
# 4  
Old 09-02-2018
Now, that's what I'd call taking the scenic route! And, it doesn't yield the desired output. What be wrong with RavinderSingh13's proposal?
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Nth field should have space and rest commas

Hello I was working on a script where the output of my file is 1234 4567 8973 43214 78965 I need the value in below format of this file.The nth field should have space instead of ,(comma) 1234,4567,8973,43214 78965 I tried the code but not working completely xargs <temp_PP.7250... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "Ç"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 Ç890000000 Ç543209911 ÇCHNGOHG Ç000000001 Ç055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

4. Shell Programming and Scripting

Remove Space and blank line from file in UNIX shell script

I have below file. I want to remove space at begining of every line and then after also remove blank line from file. I use below code for each operation. sed -e 's/^*//' < check.txt > check1.txt sed '/^\s*$/d' < check1.txt > check2.txt above code not remove all the space... (12 Replies)
Discussion started by: Mohin Jain
12 Replies

5. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

6. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

7. Shell Programming and Scripting

remove space of each line

Hi Guys, I want remove starting space of each line ... My Input: A B C D E C V F G H F R T Y U D F G H J L O I U Y G P O K O P L O L O I P P O P P P P P My Output: A B C D E C V F G H (7 Replies)
Discussion started by: asavaliya
7 Replies

8. Shell Programming and Scripting

How to remove tab space and new line from a file using sed?

i am using the default sed package that comes with solaris. (11 Replies)
Discussion started by: chidori
11 Replies

9. Shell Programming and Scripting

Remove line starting from space till end.

Hi, I have a code tag, from which i have the below snippet: intelrpt.GetCMB_FB type=ODBC> intelrpt.GetCMB_FB type=SYBASE> I want the output like: intelrpt.GetCMB_FB intelrpt.GetCMB_FB That is remove the lines starting from WHITESPACE till end. Please help. I am new to... (7 Replies)
Discussion started by: anupdas
7 Replies

10. Shell Programming and Scripting

Remove all words after first space from each line

My file looks like: asd absjdd sdff vczxs wedssx c dasx ccc I need to keep asd sdff wedssx dasx How do I do that experts?:wall::wall: (1 Reply)
Discussion started by: hakermania
1 Replies
Login or Register to Ask a Question