How to delete first elements from files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete first elements from files?
# 1  
Old 01-23-2015
How to delete first elements from files?

Hello,

I want to select first element from 2 to 3 files how I would do that. e.g my input files are:-
Code:
file1=(1,2,3,4)
file2 =(1,2,3,4)
file3=(1,2,3,4)

output should be
Code:
outputfile=(1,1,1,1)


Last edited by rbatte1; 01-23-2015 at 08:34 AM.. Reason: Added CODE tags
# 2  
Old 01-23-2015
Hello shazo,

Welcome to forums, please do use code tags for commands/codes/Inputs you are using in your posts as per forum rules, regarding your question following may help you in same.
Code:
awk -F, '{X=X?X OFS $1:$1} END{print X}' OFS=, fIle1 fIle2 fIle3 > OUTPUT_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-23-2015
Thank you ! Yeah sure I will do use code tags. This is reading whole files not first entity. Anyway thanks.
Code:
awk -F, '{X=X?X OFS $1:$1} END{print X}' OFS=, fIle1 fIle2 fIle3 > OUTPUT_file

Moderator's Comments:
Mod Comment CODE tags need to be placed around sample input, sample output, and sample code; not around your entire post.

Last edited by Don Cragun; 01-25-2015 at 03:04 PM.. Reason: Added CODE tags and corrected spelling and case
# 4  
Old 01-23-2015
Hello shazo,

Could you please try following commands and let me know if this helps you.
Code:
cat fIle1 fIle2 fIle3 > OUTPUT_file
OR
awk '1' fIle1 fIle2 fIle3 > OUTPUT_file
OR
cat fIle{1,2,3} > OUTPUT_file   ##In Bash, ksh, zsh
OR
awk '1' fIle{1,2,3} > OUTPUT_file   ##In Bash, ksh, zsh

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-23-2015 at 09:13 AM.. Reason: Added 2 more solutions
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 01-23-2015
it still reading whole file.
# 6  
Old 01-23-2015
Quote:
Originally Posted by shazo
Hello,

I want to select first element from 2 to 3 files how I would do that. e.g my input files are:-
Code:
file1=(1,2,3,4)
file2 =(1,2,3,4)
file3=(1,2,3,4)

output should be
Code:
outputfile=(1,1,1,1)

I am completely lost trying to figure out what you're trying to do.

What OS and shell are you using?

You talk about input files, but you don't give us any indication of what is in those files.
You show us code that seems (if you're using bash or ksh) to be setting array variables file1 and file3 to arrays with one element (which is the string 1,2,3,4) and invoking the utility named file2 with one operand (which is the string =(1,2,3,4)).

You seem to have three inputs, but your output seems to contain four elements. Where did the 4th element come from?

Please give us a much clearer explanation of what you are trying to do!
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 01-23-2015
i am using Linux and bash script. sorry it is my mistake. the output file should be
Code:
(1,1,1)


Last edited by Don Cragun; 01-25-2015 at 03:05 PM.. Reason: Fix CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

3. UNIX for Dummies Questions & Answers

Rename multiple files in shell bash, changing elements order.

Hi, I want to rename several files like this: example: A0805120817.BHN A0805120818.BHN ..... to: 20120817.0805.N 20120818.0805.N ...... How can i do this via terminal or in shell bash script ? thanks, (6 Replies)
Discussion started by: pintolcv
6 Replies

4. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

5. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

6. Shell Programming and Scripting

PHP: how can I delete empty/NULL elements from a multi-dimensional array.

Hi all I have a file that i'm running and exec(cat ./dat) against..and putting its contents into any array, then doing an exploding the array into a multi-dimension array... The 15 multi-dimensional arrays have elements that are null/empty, I would like to remove/unset these elements and then... (2 Replies)
Discussion started by: zeekblack
2 Replies

7. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

8. Shell Programming and Scripting

Using unset to delete array elements

Hi, I am writing a BASH script. My questions regard deleting elements of arrays. I have an array: michael-browns-powerbook-g4-15:~ msb65$ test_array=(1 2 3 4) michael-browns-powerbook-g4-15:~ msb65$ echo ${test_array} 1 2 3 4 To delete the second element of test_array I type:... (3 Replies)
Discussion started by: msb65
3 Replies

9. Shell Programming and Scripting

Average of elements throught multiple files

Hi, I got a lot of files looking like this: 1 0.5 6 All together there are ard 1'000'000 lines in each of the ard 100 files. I want to build the average for every line, and write the result to a new file. The averaging should start at a specific line, here for example at line... (10 Replies)
Discussion started by: chillmaster
10 Replies

10. Shell Programming and Scripting

find common elements in 2 files (for loop)

Hi, i'm new here (and to scripting too). I was hoping for some help in comparing two files. i have a file called 'file1' with a list of names in the following format: adam jones paul higgins kelly lowe i also have another file which may contain some of the names but with a lot of... (4 Replies)
Discussion started by: ibking
4 Replies
Login or Register to Ask a Question