Delete columns with a specific title XXX, where the position change in each file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Delete columns with a specific title XXX, where the position change in each file
# 1  
Old 12-19-2016
Delete columns with a specific title XXX, where the position change in each file

Goodmorning,

I know how to cut a string and a column, and how to find a word.

I have a file with over 100 columns. All columns have a title in the first line. I have to delete all columns with the XXX title.

I can't use cut -f because the position of XXX columns change in each file, and in each file I have many of them.

That mean that I have to find the columns XXX and then to delete them... I think Smilie
Which is the bridge between these function?

I'm not expert but in general I can understand.

Code:
Ubuntum, Bash version: 4.3.46
Bash, Perl

The position of XXX columns change in each file

Input file

aaa bbb XXX ddd XXX XXX   <-- Title
123 afa 133 2e2 dqd 24f
134 feg 566 5tf erg fe4
546 rgr 135 g5r hyt grt


Output file

aaa bbb ddd   <-- Title
123 afa 2e2
134 feg 5tf
546 rgr g5r

Thank a lot.
manolis

Last edited by rbatte1; 12-28-2016 at 06:21 AM.. Reason: Added ICODE tags
# 2  
Old 12-19-2016
Welcome to the forum.

It is always helpful to complete a request with system info like OS and shell, preferred tools, and adequate sample input and output data to avoid ambiguities and keep people from guessing.
In these fora, similar problems (identifying a column in a file and modify it) have been discussed and solved umpteen times. Did you consider searching for and adapting these? The links at the bottom of this page might be a good start.
# 3  
Old 12-19-2016
Dear moderator, thanks for your tips!

I hope now that my post is ok.

Before this post I tried to use the "search" buttom and I find many other post but not that I was looking for.

Please, if you have any url were I can find my answers, please report it in this post and then you can delete it.

Thanks a lot!
manolis

Last edited by echo manolis; 12-19-2016 at 08:34 AM..
# 4  
Old 12-19-2016
This OR this might point you in the right direction to find the column(s). Instead of printing them, remove them.
These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 12-19-2016
Hi.

This looks like a CSV-like problem, perhaps more generally and precisely, a delimiter-separated values (also DSV) format problem: Delimiter-separated values - Wikipedia. So here is a solution that uses command csvtool:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate elimination of some fields, csvtool.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C csvtool pass-fail

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

# Extract names, remove XXX, separate remainder with commas.
keeping=$( head -1 $FILE | sed 's/ XXX//g ; s/ /,/g' )
pl " Keeping these columns:" $keeping

pl " Results:"
csvtool -t " " -u " " namedcol $keeping  $FILE |
tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
bash GNU bash 4.3.30
csvtool - ( /usr/bin/csvtool, 2014-08-06 )
pass-fail (local) 1.9

-----
 Input data file data1:
aaa bbb XXX ddd XXX XXX
123 afa 133 2e2 dqd 24f
134 feg 566 5tf erg fe4
546 rgr 135 g5r hyt grt

-----
 Expected output:
aaa bbb ddd
123 afa 2e2
134 feg 5tf
546 rgr g5r

-----
 Keeping these columns: aaa,bbb,ddd

-----
 Results:
aaa bbb ddd
123 afa 2e2
134 feg 5tf
546 rgr g5r

-----
 Verify results if possible:

-----
 Comparison of 4 created lines with 4 lines of desired results:
 Succeeded -- files (computed) f1 and (standard) expected-output.txt have same content.

We first get the header names, eliminate all XXX, collect into a CSV string, and tell csvtool to produce those columns (fields) that correspond to the names we kept.

Details for csvtool:
Code:
csvtool tool for performing manipulations on CSV files from sh... (man)
Path    : /usr/bin/csvtool
Version : - ( /usr/bin/csvtool, 2014-08-06 )
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help    : probably available with --help
Home    : https://github.com/Chris00/ocaml-csv

Install from repository when you can, otherwise see csvtool home as noted above.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 12-20-2016
Oh my god !!!

I thought that was simply... ok, now I will try!

Thanks a lot guys!
# 7  
Old 12-20-2016
How about
Code:
awk -vRM="XXX" 'NR == 1 {for (i=1; i<=NF; i++) if ($i == RM) DL[i]} {for (i in DL) $i = ""; gsub (FS FS, FS)} 1' file
aaa bbb ddd
123 afa 2e2
134 feg 5tf
546 rgr g5r

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

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

3. Shell Programming and Scripting

Delete character on specific position

Hi, im still new in unix. i want to ask how to delete character on specific position in line, lets say i want to remove 5 character from position 1000, so characters from position 1000-1005 will be deleted. i found this sed command can delete 4 characters from position 10, but i dont know if... (7 Replies)
Discussion started by: bluesue
7 Replies

4. Shell Programming and Scripting

position specific replace in file

How to replace the position specific values in the file.. i searched a lot the forums but i couldn't able to do... i have file like below 576666666666666666666666666 7878 897987 121 0asdas Y12 5900fbb 777 09JJJ 78798347892374 234234234364 234232898 89HJHIHIGIUG989902743748327khjkhkjlh... (6 Replies)
Discussion started by: greenworld123
6 Replies

5. UNIX for Dummies Questions & Answers

How to delete all columns that start with a specific value

I have this space delimited large text file with more than 1,000,000+ columns and about 100 rows. I want to delete all the columns that start with NA such that: File before modification aa bb cc NA100 dd aa b1 c2 NA101 de File after modification aa bb cc dd aa b1 c2 de How would I... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. Shell Programming and Scripting

Read columns from file by position

Hello , i have a fixed-length record file where each column has a specific position. how can retrive two or more column based on their positions in the file ? Thank you (5 Replies)
Discussion started by: alain.kazan
5 Replies

7. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

8. Shell Programming and Scripting

Add characters at specific position in file

Hello I want to add some value at the specific position. My file has data like Hello Welcome to UNIX Forums Need Assistance I want to add some value at the end but at same character position for all lines. I want my output file to have data like : Here '_' represents blanks.... (3 Replies)
Discussion started by: dashing201
3 Replies

9. UNIX for Dummies Questions & Answers

Script to change/find/delete/install a specific file

Hi Very much a newbie to UNIX & scripting, but have identified an area within work that would benefit from being automated, as its repeated manually very often, and it looks like the ideal first script! What I need to do is change directory to a users home (cd ~), and then find and remove a... (6 Replies)
Discussion started by: Great Uncle Kip
6 Replies

10. Shell Programming and Scripting

Delete a file from XXX.tar.Z

Hi All can u please let me know how to delete a file from XXX.tar.Z file with out uncompressing this file. thanks in advance. --Bali (0 Replies)
Discussion started by: balireddy_77
0 Replies
Login or Register to Ask a Question