Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
rc11,rc12,rc13,rc14,rc15, etc.
,rc21,rc22,rc23,rc24,rc25, etc.
,rc31,rc32,rc33,rc34,rc35, etc.
,rc41,rc42,rc43,rc44,rc45, etc.
rc51,rc52,rc53,rc54,rc55, etc.
etc.
etc.
etc.
Trimming commas and removing leading spaces would be easy but what actually confuses me is the consistency of the columns that a data from column Z would later become column T but on another row another data from the same column would reside on column G. That is what could happen if you trim commas that way. The script I made was actually done to conform with the original in which every field is separated with a comma with an extra space after it.
---- Add ----
So do you really mean to trim out commas?
Last edited by konsolebox; 09-24-2012 at 10:11 PM.
Konsolebox, it's on a private LAN, but I'll see if I can post some of the actual data inputs and output later today.
as to removing comas, only those that are "multiples". In other words, if I use a text editor to remove all occurrences of ,, and replace them with just one , and repeat this replacing over and over again, eventually the output file will end up with something like:
Code:
rc11,rc12,rc13,rc14,rc15, etc.
,rc21,rc22,rc23,rc24,rc25, etc.
,rc31,rc32,rc33,rc34,rc35, etc.
,rc41,rc42,rc43,rc44,rc45, etc.
rc51,rc52,rc53,rc54,rc55, etc.
etc.
etc.
etc.
Yes and like I said are you not concerned if data from one column is repositioned to another column all because a part of the columns to the left would be deleted?
For example.
Code:
1,2,3,4,5
1,2,3,,5
1,,3,4,5
Suppose I am to remove the first column, the output would be like this:
Code:
2,3,4,5
2,3,5
3,4,5
Notice how the columns are no longer consistent with each other.
Code:
2 3 4 5
2 3 5
3 4 5
Last edited by konsolebox; 09-25-2012 at 07:51 AM.
note there are actually 54 columns of data, I'll only type 15 because there is nothing unique about the rest of the columns
also the end of each line does not have any spaces just a carriage return
I'll also represent the number of digits in a value as counting numbers example 12345 would represent a number that has 5 digits (this may help you in counting)
so it didn't delete the correct columns, but in the columns it deleted it did the deletion correctly!
WOOOHOOOOO,I FIGURED IT OUT the script is giving output as though the file is zero based
so the listing of to delete columns REMOVE=( 1 2 4 6 7 9)
lining them up shows whiich columns deleted 2,3,5,7,8,10 and which columns are kept 1,4,6,9,12,13,14,15
solving this problem was a simple one line change in konsolebox's script
Sorry, indices start with 0 so you had to start your columns from 0, not 1. I guess this was my mistake that I didn't notice. But if you're going to start from 1, here's a modification:
Code:
#!/bin/bash
# Change columns here. This is repetitive within the loop, but just for convenience.
REMOVE=(1 2 4 6 7 9)
# Extension of output file's name.
OUTPUTEXT='out'
IFS=','
for FILE; do
while read LINE; do
FIELDS=() # just to be safe
read -a FIELDS <<< "${LINE//, /,}"
for I in "${REMOVE[@]}"; do
unset "FIELDS[$I - 1]"
done
LINE="${FIELDS[*]}"
echo "${LINE//,/, }"
done < "$FILE" > "$FILE.$OUTPUTEXT"
done
#!/bin/bash
# Change columns here. This is repetitive within the loop, but just for convenience.
REMOVE=(1 2 4 6 7 9)
# Extension of output file's name.
OUTPUTEXT='out'
IFS=','
shopt -s extglob
for FILE; do
while read LINE; do
FIELDS=() # just to be safe
read -a FIELDS <<< "${LINE//*([[:blank:]]),*([[:blank:]])/,}"
for I in "${REMOVE[@]}"; do
unset "FIELDS[$I - 1]"
done
echo "${FIELDS[*]}"
done < "$FILE" > "$FILE.$OUTPUTEXT"
done
If you intend to clean up multiple instances of commas (,,) as well.
This would certainly break consistency within the columns so use it at your own risk.
Code:
#!/bin/bash
# Change columns here. This is repetitive within the loop, but just for convenience.
REMOVE=(1 2 4 6 7 9)
# Extension of output file's name.
OUTPUTEXT='out'
IFS=','
shopt -s extglob
for FILE; do
while read LINE; do
FIELDS=() # just to be safe
read -a FIELDS <<< "${LINE//*([[:blank:]]),*([[:blank:]])/,}"
for I in "${REMOVE[@]}"; do
unset "FIELDS[$I - 1]"
done
for I in "${!FIELDS[@]}"; do
[[ ${FIELDS[I]} == *([[:blank:]]) ]] && unset "FIELDS[$I]"
done
echo "${FIELDS[*]}"
done < "$FILE" > "$FILE.$OUTPUTEXT"
done
And this is the output if you don't remove any column (REMOVE=()). Spaces and empty instances are just trimmed out. Notice how data are no longer in their proper columns.
#!/bin/bash
[[ BASH_VERSINFO -ge 3 ]] || {
echo "You need bash version 3.0 or higher to run this script."
exit 1
}
# Set columns to keep (columns starts from 1)
KEEP_CONFIG=(1 2 4 6 7 9)
# Extension of output file's name.
OUTPUTEXT='out'
IFS=','
KEEP=()
for I in "${KEEP_CONFIG[@]}"; do
(( J = I - 1 ))
KEEP[J]=$J
done
for FILE; do
while read LINE; do
FIELDS=() # just to be safe
read -a FIELDS <<< "${LINE//, /,}"
for I in "${!FIELDS[@]}"; do
[[ -z ${KEEP[I]} ]] && unset "FIELDS[$I]"
done
LINE="${FIELDS[*]}"
echo "${LINE//,/, }"
done < "$FILE" > "$FILE.$OUTPUTEXT"
done
Or
Code:
#!/bin/bash
[[ BASH_VERSINFO -ge 3 ]] || {
echo "You need bash version 3.0 or higher to run this script."
exit 1
}
# Set columns to keep (columns starts from 1)
KEEP_CONFIG=(1 2 4 6 7 9)
# Extension of output file's name.
OUTPUTEXT='out'
IFS=','
KEEP=()
for I in "${KEEP_CONFIG[@]}"; do
(( J = I - 1 ))
KEEP[J]=$J
done
shopt -s extglob
for FILE; do
while read LINE; do
FIELDS=() # just to be safe
LINE=${LINE//*( ),*( )/,}
read -a FIELDS <<< "${LINE%%*( )}"
for I in "${!FIELDS[@]}"; do
[[ -z ${KEEP[I]} ]] && unset "FIELDS[$I]"
done
echo "${FIELDS[*]}"
done < "$FILE" > "$FILE.$OUTPUTEXT"
done
there's another thing I would like to do with the same file, it has to do with removing rows, but it's a little tricky, would you be willing to help me on that? if so should I just start a new thread or what?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.