0

I have an array of list and a file file1.txt , I want to create e new file order according to numbers in file1.txt, for example code output must be like below. How should I make it work correctly

newfile.txt 9 Z A C

but script gives me newfile.txt 2 3 4 9

file1.txt's content is like below random numbers 2 3 4 1

list=("Z" "A" "C" 9)

while read line
do
for i in ${list[@]};do

sed  "${line}s/.*/${i}/" file1.txt > newmfile.txt

done

done < "file1.txt"
5
  • What are the contents of the array? Commented Feb 1, 2021 at 9:24
  • 2 3 4 1 file1.txt and list is "Z" "A" "C" "9" so I want to print a new fiel in order 9 Z A C Commented Feb 1, 2021 at 10:10
  • With echo, head, tail or sed commands Commented Feb 1, 2021 at 10:11
  • Still unclear. Are you sure you're not expecting A C 9 Z for 2 3 4 1? Like A is the second element, C is the third, 9 is the fourth, and Z is the first. Commented Feb 1, 2021 at 12:06
  • let me clear Z A C 9 must be in order to according to 2 3 4 1 so final output must be 9 Z A C Commented Feb 1, 2021 at 12:44

3 Answers 3

2

The main issues with the script is that the sed statement needs /../ around $line. Without this, sed will do the substitution on the line number and will not search for the line. Also, as the changes aren't being retained in the original file1.txt file, only the last change takes place. You will therefore need to use the sed -i flag to execute the changes on the actual file as opposed to redirecting the changes. Additionally, having two loops is inefficient. Just use a counter and reference the elements of the array with the counter:

#!/bin/bash
list=("Z" "A" "C" 9)
cp file1.txt newmfile.txt
cnt=0
while read line 
do
   sed -i "/${line}/s/.*/${list[$cnt]}/" newmfile.txt
   cnt=$(($cnt+1))
done < "file1.txt"
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for answer but it wirtes newmfile.txt Z A C 9, but I want it to be according to order file1.txt which includes 2 3 4 1 so correct order is 9 Z A C, numbers in file1.txt is paragraph number which I want to write newmf'ile.txt
Can you be clearer, what's the logic behind 2,3,4,1? Are these the indexes of the array? If so, shouldn't it ben A C 9 Z?
random number in length of list, just think a simpe txt file and 1th paragrag is 2, second is 3 third is 5 and fourth is 1, then i want to write my list(Z A C 9) to newfile.txt in order to 1th => 9 2th => Z 3th=> A 4th=> C
Hi Raman I just replaced "/${line}/s/.*/${list[$cnt]}/" to "${line}s/.*/${list[$cnt]}/" and now it workd, thak you for your effords, how can i make it solved.
To accept the answer, follow this link stackoverflow.com/help/someone-answers
1

Your file1.txt contains the mapping:

$ nl file1.txt
     1  2
     2  3
     3  4
     4  1

where the line numbers in the above output should be replaced by the contents of your array, and reordered according to the number from the file.

Here is one method, assuming none of the array elements contain newline characters:

list=("Z" "A" "C" 9)

printf '%s\n' "${list[@]}" | paste file1.txt - | sort -nk1,1 | cut -f2-

This uses paste, sort, cut to decorate-sort-undecorate. Output:

9
Z
A
C

Comments

0

The number nine is the value of the for loop $i. the for loop continues 4 times as you've declared it to run to each item in the array.

for i in ${list[@]} 

uses number of items in array of

list=("Z" "A" "C" 9)

What is the end goal for what you want to achieve in the new text file? because it seems you're trying to base the number from the file1.txt on the array? If you just wanted to copy the contents of the old file to a new one - could be achieved easily with echo or cat. But I'm guessing that's not what you're after.

4 Comments

I want to achive that there is numbers in file1.txt like 4 2 1 3 and want to create new file from this list ( "Z" "A" "C" 9 ) but in order to file1.txt like newFile.txt => C A 9 Z
so you want to respond the array letter from the same value from the file1.txt? "1:Z" "2:A" "3:C"
no like this 1th C 2th A 3th 9 4th Z but numbers are paragraphs.
Raman Sailopal has given an answer in great detail. I think that's what you're after. be sure to tick the answer to show it fulfilled your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.