UNIX trick or command

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers UNIX trick or command
# 1  
Old 02-09-2017
UNIX trick or command

Hi,

Is there any command to do this --

Input is --
Code:
Ant
Bat
Cat
Dog

Output is --
Code:
A_Ant
B_Ant
A_Bat
B_Bat
A_Cat
B_Cat
A_Dog
B_Dog

Thanks
# 2  
Old 02-09-2017
Hello Indra2011,

Could you please try following and let me know if this helps you.
Code:
awk '{print "A_" $0 ORS "B_" $0}'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-09-2017
Shell:
Code:
while read line; do
  for i in A B; do
    echo "${i}_${line}"
  done
done < infile

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-09-2017
Thanks both of you. It worked .I wonder things are so simple for you guys
# 5  
Old 02-09-2017
Quote:
Originally Posted by Scrutinizer
Shell:
Code:
while read line; do
  for i in A B; do
    echo "${i}_${line}"
  done
done < infile

Hello Scrutinizer,

Just for fun, we could remove 1 loop from above code and could try following.
Code:
while read line
do
    echo -e "A_$line\nB_$line";
done < "Input_file"

Thanks,
R. Singh
# 6  
Old 02-09-2017
Indeed Ravinder.
One note: the portable (Posix) way of doing this would be:
Code:
printf "A_%s\nB_%s\n" "$line" "$line"

# 7  
Old 02-09-2017
And printf is even portable to awk
Code:
awk '{printf "A_%s\nB_%s\n", $0, $0}' Input_file

---------- Post updated at 16:56 ---------- Previous update was at 16:27 ----------

A sed multi-liner
Code:
sed '
s/.*/A_&\
B_&/
' Input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Vi search starting from line n --- any trick for this ???

Hi all, It does not seem to be possible to use vi to search from text from line n forward or backward or is there a not well known vi trick to do that? What I am trying to do is for example I am on line 100 and I want to search from line 100 backward or forward for a specific text. Using /... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Programming

Oracle TRICk Question

HI Guys , Below are the two columns ITEMS and STATE of table . ITEMS STATE '2345','ggdh','k5tg','dgyt','hFF' DF '1234','ghyt','DDD','GHTD','ABF' PQ Can we get output in PL/SQL in below way ?... (7 Replies)
Discussion started by: Perlbaby
7 Replies

3. UNIX for Dummies Questions & Answers

alternative to the grep trick

Hi, We used to use the below commands often. ps -ef|grep bc ps -ef|grep abc|grep -v grep Both fairly returns the same result. For example, the process name is dynamic and we are having the process name in a variable, how we can apply the above trick. For example "a" is the... (11 Replies)
Discussion started by: pandeesh
11 Replies

4. UNIX for Dummies Questions & Answers

A trick to avoid ESC in vim

Hello, I find a trick to avoid pressing ESC without key-maping in vim. I am pleasure using this method, because ALT key is very comfortble for thumb to press. What's the trick? the ALT key. When you are in INSERT mod, press ALT+l switch to COMMAND mod without... (2 Replies)
Discussion started by: vistastar
2 Replies

5. Shell Programming and Scripting

Any trick to speed up script?

Hi Guys, I have a script that I am using to convert some text files to xls files. I create multiple temp. files in the process of conversion. Other than reducing the temp. files, are there any general tricks to help speed up the script? I am running it in the bash shell. Thanks. (6 Replies)
Discussion started by: npatwardhan
6 Replies

6. Shell Programming and Scripting

Trick to ignore space in folder name

Hello All, I am getting error while passing a folder name that has space to the cmd line argument. sh log_delete2.sh "/home/kumarpua/TESTARTIFACTS/atf-chix/ATF-subversion-dev/ssenglogs/A RM" log_delete2.sh: line 17: cd: /home/kumarpua/TESTARTIFACTS/atf-chix/ATF-subversion-dev/ssenglogs/A:... (3 Replies)
Discussion started by: pulkit
3 Replies

7. UNIX for Dummies Questions & Answers

Looking for tips ant trick extend vi

Hello im looking for sites or tutorials how to extend vi (not vim) for programming and scripting beyond simple editing . Thanks allot (0 Replies)
Discussion started by: umen
0 Replies

8. Shell Programming and Scripting

Stupid find trick

At work, we use a software development product (from a company that will remain nameless, but whose name may be considered a synonym for "logical"). The development trees are organized beneath a top directory, let's call it "$rat". The first level under $rat contains the major system names, and... (2 Replies)
Discussion started by: criglerj
2 Replies
Login or Register to Ask a Question