Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Loop through directory and print on line Post 303033448 by cmccabe on Friday 5th of April 2019 12:45:15 PM
Old 04-05-2019
Loop through directory and print on line

In the bash below I am trying to loop through all the R1.gz in a directory (always 1), store them in ARRAY, and cut them before the second _. That is being done but I can't seem to print then one a single line seperated by a space. Is the below the best way or is there a better solution? Thank you Smilie.

Code:
# create an array with all the fastq.gz in directory
ARRAY=(/path/to/*R1*.gz)  ## grab only the R1 and store in ARRAY

# iterate through array
for f in "${array[@]}"; do  ## loop through ARRAY
   bname=$(basename "$f")  ## strip path from ARRAY
     s=($(echo $bname|cut -d_ -f1,2)) # remove after second underscore
   echo ${s[@]}   --- also tried "${s//$'\n'/ }" ## print on single line
done  ## end loop

files in /path/to
Code:
00-0000-xxx_S7_R1_000.gz
00-0000-xxx_S7_R2_000.gz
00-0001-xxx_S1_R1_000.gz
00-0001-xxx_S1_R2_000.gz
00-0002-xxx_S2_R1_000.gz
00-0000-xxx_S2_R2_000.gz

current
Code:
00-0000-xxx_S7 
00-0001-xxx_S1 
00-0002-xxx_S2

desired
Code:
00-0000-xxx_S7 00-0001-xxx_S1 00-0002-xxx_S2

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

2. UNIX for Dummies Questions & Answers

loop? print max column in each line for 800 files and merge

Hello, I have 800 or so files with 3 columns each and >10000 lines each. For each file and each line I would like to print the maximum column number for each line. Then I would like to 'paste' each of these files together (column-wise) so that the file with expression in label '_1' is the... (6 Replies)
Discussion started by: peanuts48
6 Replies

3. UNIX for Dummies Questions & Answers

Loop through directory and extract sub directory names

I am trying to loop through folders and extract the name of the lowest level subfolder I was running the script below, it returns /bb/bin/prd/newyork /bb/bin/prd/london /bb/bin/prd/tokyo I really want newyork london tokyo I couldn't find a standard variable for the lowest level... (1 Reply)
Discussion started by: personalt
1 Replies

4. Shell Programming and Scripting

Print in New line in loop

Hi , i want to print the output in line by line while read LINE do echo $LINE | grep UCM | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRBr | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRP| egrep '(Shutdown|Unavailable)' echo $LINE | grep OM | grep JMS|... (7 Replies)
Discussion started by: navsan
7 Replies

5. UNIX for Dummies Questions & Answers

Print all the directory with no directory name mydir

directory structure: 100k server1/ab_1234567_1/mydir 500k server1/ab_1234567_2 100k server1/ab_1234567_3/mydir 100k server1/ab_1731458_9/mydir 600k server1/ab_1234569_1 100k server1/ab_1234569_4/mydir 100k server1/ab_1234510_40/mydir 800k server1/ab_1234511_1 is there any way to generate... (10 Replies)
Discussion started by: lxdorney
10 Replies

6. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

7. Shell Programming and Scripting

Print loop output on same line dynamically

Hi, I am trying to print copy percentage completion dynamically by using the script below, #!/bin/bash dest_size=0 orig_size=`du -sk $sourcefile | awk '{print $1}'` while ; do dest_size=`du -sk $destfile | awk '{print $1}'` coyp_percentage=`echo "scale=2; $dest_size*100/$orig_size"... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

8. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

9. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies
POE::Test::Loops(3pm)					User Contributed Perl Documentation				     POE::Test::Loops(3pm)

NAME
POE::Test::Loops - Reusable tests for POE::Loop authors SYNOPSIS
#!/usr/bin/perl -w use strict; use Getopt::Long; use POE::Test::Loops; my ($dir_base, $flag_help, @loop_modules, $flag_verbose); my $result = GetOptions( 'dirbase=s' => $dir_base, 'loop=s' => @loop_modules, 'verbose' => $flag_verbose, 'help' => $flag_help, ); if ( !$result or !$dir_base or $flag_help or !@loop_modules ) { die( "$0 usage: ", " --dirbase DIR (required) base directory for tests ", " --loop MODULE (required) loop modules to test ", " --verbose show some extra output ", " --help you're reading it ", ); } POE::Test::Loops::generate($dir_base, @loop_modules, $flag_verbose); exit 0; DESCRIPTION
POE::Test::Loops contains one function, generate(), which will generate all the loop tests for one or more POE::Loop subclasses. The "SYNOPSIS" example is a version of poe-gen-tests, which is a stand-alone utility to generate the actual tests. poe-gen-tests also documents the POE::Test::Loops system in more detail. FUNCTIONS
generate( $DIRBASE, @LOOPS, $VERBOSE ) Generates the loop tests. DIRBASE is the (relative) directory in which a subdirectory for each of the LOOPS is created. If VERBOSE is set to a TRUE value some progress reporting is printed. POE::Test::Loops::generate( "./t", [ "POE::Loop::Yours" ], 1, ); SEE ALSO
POE::Loop and poe-gen-tests. AUTHOR &; COPYRIGHT See poe-gen-tests. perl v5.14.2 2012-03-11 POE::Test::Loops(3pm)
All times are GMT -4. The time now is 03:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy