sed or awk to remove specific column to one range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed or awk to remove specific column to one range
# 1  
Old 01-25-2019
sed or awk to remove specific column to one range

I need to remove specific column to one range

source file

Code:
3 1 000123456
2 2 000123569
3 3 000123564
12 000123156
15 000125648
128 000125648

Output required
Code:
3 000123456
2 000123569
3 000123564
12 000123156
15 000125648
128 000125648


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-25-2019 at 03:25 PM.. Reason: Changed CODE tags.
# 2  
Old 01-25-2019
Code:
sed -r 's/\s\w+\s/ /' file

# 3  
Old 01-25-2019
But this one removing all the second column. I need to remove only up to three lines. After third i need the two column
# 4  
Old 01-25-2019
Quote:
Originally Posted by ranjancom2000
But this one removing all the second column. I need to remove only up to three lines. After third i need the two column
Wouldn't it be nice to post a complete specification in the first place, then? And, to show your own ideas and attempts?


Try an awk approach:


Code:
awk 'NF == 3 {$2 = ""} 1' file
3  000123456
2  000123569
3  000123564
12 000123156
15 000125648
128 000125648

The refinements to eliminate the remaining two adjacent field separators are left as an exercise to the reader.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-25-2019
it was working fine thanks
# 6  
Old 01-25-2019
according to the problem statement
Code:
sed -r 's/\s\w+\s/ /' file
3 000123456
2 000123569
3 000123564
12 000123156
15 000125648
128 000125648

the desired range is set simply and the editor ignores the remaining lines out of range
Code:
echo 1 2 3 4 5 >> file
sed -r '1,3s/\s\w+\s/ /' file
3 000123456
2 000123569
3 000123564
12 000123156
15 000125648
128 000125648
1 2 3 4 5

# 7  
Old 01-25-2019
If the sample data shown in post #1 is representative of the actual data that is to be processed and you don't want the extra <space> in the output produced by changing the field 2 value to an empty string, one could also try:
Code:
awk '{print $1, $NF}' file

which just prints the first and last field from each input line.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern. Say file input is MYNUMBERD000000-BAN CHUE INSNTS ** N+ MYAREDSDD000000+BAN CHUE INSNTS ** N+ MYDERFFFSD00000-GIR PENT - ACH ** ... (5 Replies)
Discussion started by: navojit dutta
5 Replies

3. Shell Programming and Scripting

Remove Specific Column in a File using awk

Hi, I would like to ask your expertise to remove specific column no. 8 in the below file using but I don't have an idea on how to simply do this using awk command. Appreciate your help in advance. Input f: ABC 1 1XC CDA 1 2YC CCC 1 3XC AVD 1 3XA Expected output file: ABC 1 1C CDA... (9 Replies)
Discussion started by: zzavilz
9 Replies

4. Shell Programming and Scripting

awk or sed to find specific column from different files

Hi everybody, I have a folder with many files: Files with 8 columns: X 123 A B C D E F And files with 7 columns: X1234 A B C D E F I am trying to find a way to extract the 5th column when the files have eight columns, or the 4th column when the files have... (3 Replies)
Discussion started by: Tzole
3 Replies

5. Shell Programming and Scripting

Remove a range of lines from a file using sed

Hi I am having some issue editing a file in sed. What I want to do is, in a loop pass a variable to a sed command. Sed should then search a file for a line that matches that variable, then remove all lines below until it reaches a line starting with a constant. I have managed to write a... (14 Replies)
Discussion started by: Andy82
14 Replies

6. Shell Programming and Scripting

remove unwanted specific line range

Hello everyone...I have large txt file and I would like to remove unwanted specific line. My data is like this: So I would like to remove from line below No. until line reassambled like this: Thanks... (4 Replies)
Discussion started by: taxi
4 Replies

7. Shell Programming and Scripting

Remove strings within range using sed

Hey folks I have a big file that contains junk data between the tags <point> and </point> and I need to delete it (including `<point>' and `</point>'). i.e. a = 1 <point> 123123 2342352 234231 234256 </point> print a needs to become a = 1 print a I'm certain that this is a... (10 Replies)
Discussion started by: ksk
10 Replies

8. Shell Programming and Scripting

Remove certain parameters from column using awk or sed

I have a text file Nov 1 LOG_10_000000343.gzip_COMPLETE 2910 server.log.3 Nov 4 LOG_10_000000343.gzip_COMPLETE 2910 server.log.4 Dec 5 LOG_10_000000343.gzip_blah 2910 server.log.5 Jul 6 LOG_10_000000343.gzip_ERROR 2910 server.log.1 I need to convert this to Nov 1 LOG_10_000000343.gzip... (3 Replies)
Discussion started by: gubbu
3 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

10. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies
Login or Register to Ask a Question