0

I've been stuck for a few hours now. I would like to delete the comma of the last element of my table except that I don't know how to get this last element and how to delete this comma without impacting the other elements.

The elements are as follows:

DIR_PLG40AT : 145_PLG40AT.0234.tsv, DIR_PLG40AT : 146_PLG40AT.0234.tsv, 
DIR_PLG40AT : 222_PLG40AT.0234.tsv, DIR_PLG40AT : 043_PLG40AT.0234.tsv, 
DIR_PLG40AT : 045_PLG40AT.0234.tsv, DIR_PLG40AT : 193_PLG40AT.0234.tsv, 
DIR_PLG40AT : 025_PLG40AT.0234.tsv, DIR_PLG40AT : 073_PLG40AT.0234.tsv, 
DIR_PLG40AT : 232_PLG40AT.0234.tsv, DIR_PLG40AT : 161_PLG40AT.0234.tsv, 
DIR_PLG40AT : 147_PLG40AT.0234.tsv, DIR_PLG40AT : 075_PLG40AT.0234.tsv, 
DIR_PLG40AT : 245_PLG40AT.0234.tsv, DIR_PLG40AT : 256_PLG40AT.0234.tsv, 
DIR_PLG40AT : 096_PLG40AT.0234.tsv, DIR_PLG40AT : 054_PLG40AT.0234.tsv, 
DIR_PLG40AT : 259_PLG40AT.0234.tsv, DIR_PLG40AT : 154_PLG40AT.0234.tsv, 
DIR_PLG40AT : 211_PLG40AT.0234.tsv, DIR_PLG40AT : 198_PLG40AT.0234.tsv, 
DIR_PLG40AT : 164_PLG40AT.0234.tsv, DIR_PLG40AT : 213_PLG40AT.0234.tsv, 
DIR_PLG40AT : 155_PLG40AT.0234.tsv, DIR_PLG40AT : 175_PLG40AT.0234.tsv, 
DIR_PLG40AT : 092_PLG40AT.0234.tsv, DIR_PLG40AT : 212_PLG40AT.0234.tsv, 
DIR_PLG40AT : 097_PLG40AT.0234.tsv, DIR_PLG40AT : 151_PLG40AT.0234.tsv, 
DIR_PLG40AT : 011_PLG40AT.0234.tsv, DIR_PLG40AT : 021_PLG40AT.0234.tsv, 
DIR_PLG40AT : 063_PLG40AT.0234.tsv, DIR_PLG40AT : 121_PLG40AT.0234.tsv, 
DIR_PLG40AT : 171_PLG40AT.0234.tsv, DIR_PLG40AT : 234_PLG40AT.0234.tsv, 
DIR_PLG40AT : 012_PLG40AT.0234.tsv, DIR_PLG40AT : 197_PLG40AT.0234.tsv, 
DIR_PLG40AT : 052_PLG40AT.0234.tsv, DIR_PLG40AT : 074_PLG40AT.0234.tsv, 
DIR_PLG40AT : 242_PLG40AT.0234.tsv, DIR_PLG40AT : 231_PLG40AT.0234.tsv, 
DIR_PLG40AT : 095_PLG40AT.0234.tsv, DIR_PLG40AT : 243_PLG40AT.0234.tsv, 
DIR_PLG40AT : 162_PLG40AT.0234.tsv, DIR_PLG40AT : 196_PLG40AT.0234.tsv, 
DIR_PLG40AT : 148_PLG40AT.0234.tsv, DIR_PLG40AT : 179_PLG40AT.0234.tsv, 
DIR_PLG40AT : 022_PLG40AT.0234.tsv, DIR_PLG40AT : 173_PLG40AT.0234.tsv, 
DIR_PLG40AT : 165_PLG40AT.0234.tsv, DIR_PLG40AT : 014_PLG40AT.0234.tsv, 
DIR_PLG40AT : 024_PLG40AT.0234.tsv, DIR_PLG40AT : 223_PLG40AT.0234.tsv, 
DIR_PLG40AT : 093_PLG40AT.0234.tsv, DIR_PLG40AT : 041_PLG40AT.0234.tsv, 
DIR_PLG40AT : 071_PLG40AT.0234.tsv, DIR_PLG40AT : 091_PLG40AT.0234.tsv, 
DIR_PLG40AT : 144_PLG40AT.0234.tsv, DIR_PLG40AT : 235_PLG40AT.0234.tsv, 
DIR_PLG40AT : 225_PLG40AT.0234.tsv, DIR_PLG40AT : 163_PLG40AT.0234.tsv, 
DIR_PLG40AT : 233_PLG40AT.0234.tsv, DIR_PLG40AT : 122_PLG40AT.0234.tsv, 
DIR_PLG40AT : 194_PLG40AT.0234.tsv, DIR_PLG40AT : 023_PLG40AT.0234.tsv, 
DIR_PLG40AT : 051_PLG40AT.0234.tsv, DIR_PLG40AT : 143_PLG40AT.0234.tsv, 
DIR_PLG40AT : 215_PLG40AT.0234.tsv, DIR_PLG40AT : 125_PLG40AT.0234.tsv, 
DIR_PLG40AT : 251_PLG40AT.0234.tsv, DIR_PLG40AT : 176_PLG40AT.0234.tsv, 
DIR_PLG40AT : 177_PLG40AT.0234.tsv, DIR_PLG40AT : 191_PLG40AT.0234.tsv, 
DIR_PLG40AT : 252_PLG40AT.0234.tsv, DIR_PLG40AT : 254_PLG40AT.0234.tsv, 
DIR_PLG40AT : 124_PLG40AT.0234.tsv, DIR_PLG40AT : 055_PLG40AT.0234.tsv, 
DIR_PLG40AT : 253_PLG40AT.0234.tsv, DIR_PLG40AT : 056_PLG40AT.0234.tsv, 
DIR_PLG40AT : 016_PLG40AT.0234.tsv, DIR_PLG40AT : 195_PLG40AT.0234.tsv, 
DIR_PLG40AT : 142_PLG40AT.0234.tsv, DIR_PLG40AT : 042_PLG40AT.0234.tsv, 
DIR_PLG40AT : 221_PLG40AT.0234.tsv, DIR_PLG40AT : 241_PLG40AT.0234.tsv, 
DIR_PLG40AT : 065_PLG40AT.0234.tsv, DIR_PLG40AT : 013_PLG40AT.0234.tsv, 
DIR_PLG40AT : 015_PLG40AT.0234.tsv, DIR_PLG40AT : 094_PLG40AT.0234.tsv, 
DIR_PLG40AT : 258_PLG40AT.0234.tsv, DIR_PLG40AT : 026_PLG40AT.0234.tsv, 
DIR_PLG40AT : 172_PLG40AT.0234.tsv, DIR_PLG40AT : 064_PLG40AT.0234.tsv, 
DIR_PLG40AT : 199_PLG40AT.0234.tsv, DIR_PLG40AT : 072_PLG40AT.0234.tsv, 
DIR_PLG40AT : 224_PLG40AT.0234.tsv, DIR_PLG40AT : 214_PLG40AT.0234.tsv, 
DIR_PLG40AT : 152_PLG40AT.0234.tsv,

The code :

for fichier in `find $rep -type f`
do
  fichier=$(basename "$fichier")
  tmp+="$variableFinal DIR_PLG40AT : $fichier,"
done


tableau=${#tmp[*]}
for (( i=0; i<tableau; i++ )) 
do
    chaine=${tmp[i]}
done

The goal is to then pass this string to an ExternalTables script

Thanks !

miken32
  • 39,644
  • 15
  • 91
  • 133
  • `tmp` is not an array, so `${#tmp[*]}` is just a long way to write `${#tmp}`. – chepner Oct 15 '18 at 13:57
  • refer this link to remove comma from last line ..https://stackoverflow.com/questions/12390134/remove-comma-from-last-line – kanagaraj Oct 15 '18 at 14:41
  • Whatever you're doing is very unclear from your question. Is the first block of text supposed to be the output from the code? There's no output at all happening from that code. – miken32 Oct 16 '18 at 00:40
  • @miken32 That's what I would like to have in my tmp table except that I have the impression that it does not create a table. –  Oct 16 '18 at 06:40
  • @chepner How to put it in array –  Oct 16 '18 at 06:42
  • Okay, I just figured it out. Thank you for your help. Now I have a question: How can I make this string hard: DIR_PLG40AT : + NAME_FILE, repeatedly except at the end I don't want the comma –  Oct 16 '18 at 12:19
  • If you want the solutions : #CREATION DE LA CHAINE FINAL for fichier in `find $rep -type f` do fichier=$(basename "$fichier") array+="$variableFinal DIR_PLG40AT : $fichier," done array=${array:0:$(expr ${#array} - 1)} chaineFinal=${array} echo $chaineFinal –  Oct 16 '18 at 13:09
  • Just so you know, you aren't using arrays at all. It's all strings. Based on your last comment I think my answer does what you're looking for much more efficiently; you have a lot of code that doesn't do anything at all. – miken32 Oct 16 '18 at 15:21

2 Answers2

0

If you're using a modern version of bash (>= version 4.0) you can do this without find at all:

#!/bin/bash
shopt -s globstar
tmp=""
for f in **/*; do
    tmp+="DIR_PLG40AT : $(basename "$f"),"
done
echo "${tmp%,}"

Otherwise:

#!/bin/bash
tmp=""
while IFS= read -d '' -r f; do
    tmp+="DIR_PLG40AT : $(basename "$f"),"
done < <(find . -type f -print0)
echo "${tmp%,}"

This is simply appending the new value onto the string repeatedly; at the end the comma can easily be removed. The find version is a bit complicated but is safe for filenames with whitespace.


I'm assuming your files are in multiple subdirectories or you wouldn't have needed find under any circumstances and could just do this:

#!/bin/bash
tmp=""
for f in *; do
    tmp+="DIR_PLG40AT : $(basename "$f"),"
done
echo "${tmp%,}"
miken32
  • 39,644
  • 15
  • 91
  • 133
-1

sed can do the job. Is this what you are looking for?.

> cat table.dat
DIR_PLG40AT : 145_PLG40AT.0234.tsv, DIR_PLG40AT : 146_PLG40AT.0234.tsv,
DIR_PLG40AT : 222_PLG40AT.0234.tsv, DIR_PLG40AT : 043_PLG40AT.0234.tsv,
DIR_PLG40AT : 045_PLG40AT.0234.tsv, DIR_PLG40AT : 193_PLG40AT.0234.tsv,
DIR_PLG40AT : 025_PLG40AT.0234.tsv, DIR_PLG40AT : 073_PLG40AT.0234.tsv,
> sed 's/,\s*$//g' table.dat
DIR_PLG40AT : 145_PLG40AT.0234.tsv, DIR_PLG40AT : 146_PLG40AT.0234.tsv
DIR_PLG40AT : 222_PLG40AT.0234.tsv, DIR_PLG40AT : 043_PLG40AT.0234.tsv
DIR_PLG40AT : 045_PLG40AT.0234.tsv, DIR_PLG40AT : 193_PLG40AT.0234.tsv
DIR_PLG40AT : 025_PLG40AT.0234.tsv, DIR_PLG40AT : 073_PLG40AT.0234.tsv
>
stack0114106
  • 8,220
  • 3
  • 11
  • 35
  • It's a poorly worded question, but if you look at the existing code, this data is coming from a list of files in a directory, not a text file. – miken32 Oct 16 '18 at 21:43