LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-30-2016, 05:19 PM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
Question Using BASH: Move a single line of data to columns when a " "\n is found


So I have this INPUT file:
1
2
3

4
5
6

7
8
9




I want this to happen:
1 4 7
2 5 8
3 6 9

So basically read in a file with a single column values and whenever it finds a line without any character (or the " " blank character), then move the incoming values to another column.

Thanks in advance!
 
Old 09-30-2016, 10:48 PM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,552

Rep: Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433Reputation: 1433
Quick and dirty, leaving intermediate files.
Code:
split -l4 INPUT; paste x*
 
1 members found this post helpful.
Old 10-01-2016, 12:39 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,625

Rep: Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088Reputation: 2088
My normal response to posts like this is "show us what you have attempted". I might help where you have a (code) blockage, but I won't do it all for you.
And is that sample representative - always 3 data lines ?.
 
1 members found this post helpful.
Old 10-01-2016, 03:25 AM   #4
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 918
Blog Entries: 10

Rep: Reputation: 318Reputation: 318Reputation: 318Reputation: 318
Cool! This got me to learn/read/study/experiment-with/...
man split paste
(the paste may want -d" ")
Yes, "do it for me" is contrary to LQ FAQ here and buried in rules:
Quote:
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches)
(I don't mean to imply that this is a homework question)

EDIT: ooops, split operates on a fixed number of lines.
You'd need something [your bash code] that switches to a new output-file
"whenever it finds a line without any character" (regex ^$ or whitespace)
Do you know/want-to-learn awk?
awk -v RS= '{print > ("x" NR )}' INPUT
(I found it via this search:
split a file on blank lines

Last edited by Jjanel; 10-01-2016 at 04:34 AM.
 
1 members found this post helpful.
Old 10-01-2016, 02:13 PM   #5
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
#!/bin/bash



SCRIPT_NAME=`basename ${BASH_SOURCE[0]}`
#If not enough arguments...

SOURCE=$(pwd)
if [ -z $3 ] ; then
echo "Error (run it like):
$SCRIPT_NAME <all_FA_skeletonized> <FA_NAME LIST (usually from origdata/*)> <LOCATION_OF_MASKS>

1st argument --> The all_fa_skeletonized that you want to use.
2nd --> *.list (A txt/list file with the FA names located in the ./origdata folder created at tbss_1.).
3rd --> the directory name where all the masks will be applied (I'll do a ls *.gz for it...)"

exit
fi

#VARIABLE INITIALIZATION
SKEL=$1
FA_LIST=$2
MASK_DIR=$3
TABLE_OUT=${SOURCE}/FA_values.txt
TMP_FILE=$SOURCE/tmp12345.txt
MASKS_DIR=$SOURCE/masks_skel_FAs




#Removing temporal files if exist
if [ -f $TMP_FILE ] ; then
rm $TMP_FILE
fi



#CREATING ID_LIST
ID_LIST=$(cat $FA_LIST)

#Check to see whether the *.table that will have the values exists. If so, move the earlier one to the time this script was run.
DATE=$( date +"%m-%d-%Y-%HH-%MM" )
if [ -f $TABLE_OUT ] ; then
mv $TABLE_OUT ${TABLE_OUT}.$DATE
fi


#CHECK IF MASK HAVE BEEN CREATED. IF SO, MOVE THEM...
if [ -d $MASKS_DIR ] ; then
mv $MASKS_DIR ${MASKS_DIR}.${DATE}
fi
mkdir -p ${MASKS_DIR}

#RECORDING EACH MEAN VALUE AND ASSIGN IT TO THE "VALUE" ARRAY TO BE DISPLAYED IN ANOTHER LOOP
COUNTER=1


echo "Now we will start masking and outputting the mask values:"
for FILE in $( ls $MASK_DIR/*.nii.gz ) ; do
MASK=$(basename ${FILE//.nii.gz} )
echo "In $MASK ... "
fslmaths $SKEL -mul $FILE ${MASKS_DIR}/${MASK}_skelFA.nii.gz

fslstats -t ${MASKS_DIR}/${MASK}_skelFA.nii.gz -M | tee -a $TMP_FILE
echo "" | tee -a $TMP_FILE
COUNTER=$(($COUNTER+1))
done



###
# HERE IS THE CODE THAT I NEED TO RUN IN $TMP_FILE
##
 
Old 10-01-2016, 07:32 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 408Reputation: 408Reputation: 408Reputation: 408Reputation: 408
I would use an array to store values in the loop, something like:
Code:
#RECORDING EACH MEAN VALUE AND ASSIGN IT TO THE "VALUE" ARRAY TO BE DISPLAYED IN ANOTHER LOOP
COUNTER=0
declare -a LINES

echo "Now we will start masking and outputting the mask values:"
for FILE in $( ls $MASK_DIR/*.nii.gz ) ; do
  MASK=$(basename ${FILE//.nii.gz} )
  echo "In $MASK ... "
  fslmaths $SKEL -mul $FILE ${MASKS_DIR}/${MASK}_skelFA.nii.gz
  
  fslstats -t ${MASKS_DIR}/${MASK}_skelFA.nii.gz -M | while read -r value; do
    LINES[$COUNTER]=${LINES[$COUNTER]}$value" "
    ((COUNTER++))
  done
  
  COUNTER=0
done

for line in "${LINES[@]}"; do
  echo ${line::-1} >> $TMP_FILE
done

# check content in $TMP_FILE
echo "content in $TMP_FILE:"
cat $TMP_FILE
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] X: "loading extension glx" "no screens found" "fatal server error" (w/ nvidia driver) Geremia Slackware 7 12-29-2014 11:00 AM
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 06:41 PM
first line on disp during boot "doing fast boot", then "FATAL: mbcache not found" rangerpresto SUSE / openSUSE 2 02-13-2010 10:18 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically jimieee Programming 9 05-03-2006 10:24 AM


All times are GMT -5. The time now is 04:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration