Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
It saves me lot of time dragging along on this small problem. NEdit supports copy and paste column. vi also can copy column but I haven't find a way to paste the same to another file.
Anyway, lot of thanks to folks providing me assistance on my problem.
We have two "sed" search and replace commands each begins with -e.
In the sed command 's/^ //' is a search and replace function where the search is contained between the first foward slash and the second forward slash.
In this case "^ " which searches for a line that begins (^) with two spaces.
Between the second forward slash and the third forward slash are what you want to replace the two spaces with. In this case nothing.
In the second search and replace command 's/^[0-9]* //' we are searching for a line that begins (^) with 0 or more numbers followed by a space and again will replace it with (//) nothing.
So essentially what it would do based on the data set you provided is strip off the first field of information. Now "Year" becomes column/field #1 "Yield" becomes field #2 etc. etc.. These are then piped to awk and $2 followed by a tab followed by $3 are printed to your screen or could be redirected to another file.
We have two "sed" search and replace commands each begins with -e.
In the sed command 's/^ //' is a search and replace function where the search is contained between the first foward slash and the second forward slash.
In this case "^ " which searches for a line that begins (^) with two spaces.
Between the second forward slash and the third forward slash are what you want to replace the two spaces with. In this case nothing.
In the second search and replace command 's/^[0-9]* //' we are searching for a line that begins (^) with 0 or more numbers followed by a space and again will replace it with (//) nothing.
So essentially what it would do based on the data set you provided is strip off the first field of information. Now "Year" becomes column/field #1 "Yield" becomes field #2 etc. etc.. These are then piped to awk and $2 followed by a tab followed by $3 are printed to your screen or could be redirected to another file.
I hope this helps explain what the command did.
Hi Fordeck,
Thanks for your detail explanation and time.
My understanding is as follows: (If I'm wrong pls correct me)
1)
The 1st "search and replace" is to search the line beginning with "2 spaces", i.e. the 1st line and replace "2 spaces" with nothing, i.e. stripping it away
2)
The 2nd "search and replace" is to search the lines beginning with "0-9" and replace them with nothing, i.e. stripping them away.
What is the function * after [0-9] ?
What is the function of "\t" ?
A side question is it possible printing the output as a line separated with a) a space or b) with a comma ",", i.e. column becoming row?
... If this is a homework assignment and you are learning about scripting, then this answer might be more complicated and un ethical to answer here.
For example, each number is right justified with the title of each column. This means the spacing has to be real spaces and not a tab character.
The only way to transpose the columns with the same formatting would be a complicated script that will count how wide the column is supposed to be, then re align each column individually as it prints each line.
For example:
Code:
Year Yield Rainfall Temperature
1 1963 60 8 56
to trans pose Rainfall with Yield, I would count each columns width by its title:
then reprint the first column as needed by determining what needed to be swapped
echo " $COL1 $COL2 $COL3 $COL4"
then some complicated function to re print each line to columns:
# Loop all lines
echo "$LINE_NUMBER "
# Loop this line
# Don't forget to swap it as you are doing this
DATA=`awk "{print \$$COL_NUMBER}"`
CURWIDTH=<current data width>
let "SPACE_WIDTH = COLW1 - CURWIDTH"
SPACES=< some conversion from SPACE_WIDTH to actual spaces >
# Note '-n' to prevent a new line
echo -n "$SPACES $DATA "
# Some terminating check
# then echo a new line
echo
... If this is a homework assignment and you are learning about scripting, then this answer might be more complicated and un ethical to answer here.
Non of abovementioned applies to my need.
I'm learning R (R-Project) and need many datasets to plot graphs. If I can't find the dataset needed on R database I have to build it/them myself. I can find many sample dataset on Internet but some of them need modified. That is the cause starting this thread.
Quote:
For example, each number is right justified with the title of each column. This means the spacing has to be real spaces and not a tab character.
The only way to transpose the columns with the same formatting would be a complicated script that will count how wide the column is supposed to be, then re align each column individually as it prints each line.
For example:
Code:
Year Yield Rainfall Temperature
1 1963 60 8 56
to trans pose Rainfall with Yield, I would count each columns width by its title:
then reprint the first column as needed by determining what needed to be swapped
echo " $COL1 $COL2 $COL3 $COL4"
then some complicated function to re print each line to columns:
# Loop all lines
echo "$LINE_NUMBER "
# Loop this line
# Don't forget to swap it as you are doing this
DATA=`awk "{print \$$COL_NUMBER}"`
CURWIDTH=<current data width>
let "SPACE_WIDTH = COLW1 - CURWIDTH"
SPACES=< some conversion from SPACE_WIDTH to actual spaces >
# Note '-n' to prevent a new line
echo -n "$SPACES $DATA "
# Some terminating check
# then echo a new line
echo
# Some terminating check
Whether run following commands:-
Code:
sed -e 's/^ //' -e 's/^[0-9]* //' test.txt \
echo " $COL1 $COL2 $COL3 $COL4" \
echo "$LINE_NUMBER " \
DATA=`awk "{print \$$COL_NUMBER}"` \
CURWIDTH=<current data width> \ # what shall I replace "current data width"
let "SPACE_WIDTH = COLW1 - CURWIDTH" \
SPACES=< some conversion from SPACE_WIDTH to actual spaces > \ # what shall I replace On < some ..... space >
echo -n "$SPACES $DATA " \
echo
If it's a script and you use 'echo' inside of it, then you can send the output of the script to a file.
Code:
sh myscript.sh > create_and_or_overwright.txt
sh myscript.sh >> create_and_or_append.txt
My pseudo type code was aimed at keeping the format the same as the example data sheet while transposing the columns. That is, right justified numbers with each column. If this is for personal use, you could probably just save it as a csv file. I'm sure anything dealing with data points is going to understand a text file with "Comma Separated Values". The \t tab character would probably be good enough too.
It all depends then on if you need the related data points in rows or columns and all the other options mentioned above would suffice.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.