AntiGMO
AntiGMO

Reputation: 1587

shell get string

I have some lines have same structure like

1000    AS34_59329    RICwdsRSYHSD11-2-IPAAPEK-93     /ifshk5/BC_IP/PROJECT/T1
1073/T11073_RICekkR/Fq/AS34_59329/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IP
AAPEK-93_1.fq.gz        /ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_5932
9/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_2.fq.gz        /ifshk5/
BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59329/clean_111220_I631_FCC0E5EACXX_
L4_RICwdsRSYHSD11-2-IPAAPEK-93_1.fq.gz.total.info       11.824  0.981393
43.8283 95.7401 OK

And I want to get the Bold part to check whether in /home/jesse/ has this folder, if not create mkdir /home/jesse/AS34_59329

I use this code

  ! /bin/bash
  myPath="/home/jesse/"
  while read myline
  do
     dirname= echo "$myline" | awk -F ' ' '{print $2}'
     echo $dirname
     myPath= $myPath$dirname
     echo $myPath
     mkdir -p "$myPath"

  done < T11073_all_3254.fq.list

But it can't mkdir and show the path name, it shows

 -bash: /home/jesse/: is a directory
 /home/jesse/
 AS39_59324

Upvotes: 0

Views: 66

Answers (2)

that other guy
that other guy

Reputation: 123460

read can read each field into a separate variable, and mkdir -p will create a dir only if it doesn't exist:

path="/home/jesse"
while read _ dir _ 
do 
    mkdir -p "$path/$dir"
done < T11073_all_3254.fq.list

Upvotes: 1

jman
jman

Reputation: 11606

for will iterate over each whitespace separated token. Try this instead.

#!/usr/bin/env bash
# Invoke with first arg as file containing the lines
#     foo.sh <input_filename>

for i in `cat $1 | cut -d " " -f2`
do 
    if [ -d /home/jesse/$i ]
    then
        echo "Directory /home/jesse/$i exists"
    else
        mkdir /home/jesse/$i;
        echo "Directory /home/jesse/$i created"
    fi
done

Upvotes: 0

Related Questions