Perl script to remove new line character and move next line data to previous line [closed] - perl

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I have input like below
"ID"|"Desc"
"100"|"
The data present in Desc column has new line characters.
So the data came to second line.
Some records of data went to third line. But I need all data to be present in first line."
"101"|"This record desc is correct data which has present in single line. So I need data to present in single line."
I need output like below,
"ID"|"Desc"
"100"|"The data present in Desc column has new line characters.So the data came to second line.Some records of data went to third line. But I need all data to be present in first line."
"101"|"This record desc is correct data which has present in single line. So I need data to present in single line."
Can someone please help the Perl script where we can achieve above requirement.

Use Text::CSV_XS to process the file as it can parse it correctly.
perl -MText::CSV_XS=csv -wE 'csv( in => shift,
always_quote => 1,
sep_char => "|",
eol => "\n",
on_in => sub { $_[1][1] =~ s/\n//g } );
' -- file.csv > newfile.csv
I'm testing this in a Linux shell, you might need a different eol if you're in MSWin. Also, I don't know what rules Powershell uses for quoting, co you might need to use a different type of quotes.

Related

How to set a text content of a specific column as a variable using Batch or PowerShell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 months ago.
Improve this question
For example, I have a file/file output with the following content:
2022-10-16 14:33 1,860,477 Mausi-~1.JPG Mausi-wife.JPG
There are spaces between these five blocks in between, i.e. between date and time and number and file name and another file name.
I would now like to set only the fourth column of this as a variable. Is this possible?
Split the string on spaces, update the desired value in the resulting array, and then stitch back together with -join:
$row = '2022-10-16 14:33 1,860,477 Mausi-~1.JPG Mausi-wife.JPG'
# split into individual field values
$fields = $row.Split(' ')
# update/overwrite the 4th item in the resulting string array
$fields[3] = "New value"
# stitch row back together with `-join`
$row = $fields -join ' '

How to delete a specific line from file using sed [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
When I try to delete a specific line number from my file, all lines that have the same pattern are deleted. This is not what I want, I want to only delete the line number itself, not similar patterns.
Here is what I am trying to do:
x = 5
Command I run now:
sed -i "${x}d" home/file.txt
You had spaces around your variable assignment
x = 5
Which would be wrong.
Try the below fix
x=5
sed -i ${x}d home/file.txt
Here is an example to delete line number 33 of your file:
sed -i '33d' home/file.txt
If you need the number to be a variable:
local line_number=5
sed -i "${line_number}d" home/file.txt
The problem with what you are doing is the spaces, x = 5, will not work while x=5 will.
Here is what I am trying to do:
x = 5
That gives me:
bash: x: command not found...
, which is what I would expect. If you did not get a similar error message then you must be in the unfortunate situation of having a program named x in your path, perhaps because of doing something unwise, such as putting . in your PATH. Or perhaps you got such a message but did not see it because you have redirected your stderr.*
In any event, the quoted line does not assign a value to any shell variable. Shell variable assignments must not have whitespace around the = operator, so if you want to assign 5 to variable x, that must be
x=5
.
It is not an error to perform parameter expansion on a name that has not been assigned any value. The result is nothing. Thus, if x has not successfully been assigned any value then "${x}d" will expand to "d". As a complete sed script, that will delete every line.
*Or if you did get such an message then why in the world didn't you at least say so?

Rename file from xx_02.csv to xx.csv [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a folder 'a' with about 200 files with names xx_out_02.csv and I want to rename them to xx_out.csv. May be using Matlab or running some script. I tried it in cmd but I have to run the command for each and every file.
Can someone help me here?
Best Regards
Dilip
You can use the movefilefunction from matlab.
Here is an example:
clc
addpath('yourdir')
csvf = dir('yourdir/*.csv');
numberOfcsv = numel(csvf);
for ii = 1:numberOfcsv
file = csvf(ii).name;
movefile(sprintf('yourdir/%s', file), sprintf('yourdir/x%03d_out.csv', ii), 'f');
end
Your question is unclear. I'm assuming
You want to strip off substrings of the form _ followed by one or more digits right before .csv.
The resulting target names are all different. For example, you have files such as xx_out_02.csv and yy_out_01.csv, but not xx_out_02.csv and xx_out_01.csv.
Operating system? I'm considering Windows. For other systems you can change the system line below with the appropriate system comand. Or better use movefile as in SamuelNLP's answer.
Code:
files = dir('*.csv');
names = {files.name};
for n = 1:numel(names)
name = names{n};
name_new = regexprep(name, '_\d+(?=\.csv$)', '');
system(['ren ' name ' ' name_new]); %// MS-DOS command to rename file
end

How to check output from command - PERL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have a problem, I am new in Perl, so.
I want to get output from command i send to check if its done, for example:
In my module i run command my_zip_command file a_lot_of_file' and i want to my module wait until the massage 'all file were zip correctly' will be printed.
I tried get the STDOUT but it didnt work for me, or i just doing it wrong.
bash>my_zip_command file a_lot_of_file
>ziping file1 100%
>ziping file2 100%
>ziping file3 100%
>all file were zip correctly
bash>
Thanks for all your help
If you are trying to
Run a command from your perl script and
Capture the output of that command inside your perl script
this is the simplest way I can think of.
my #output = `my_zip_command file a_lot_of_file`;
#output will hold the complete output of the command.

Extract text, Matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find a way to extract text in a specific and efficient way
as in this example:
'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234"
I want a way to get the Name, the Number and the Bank ID Number.
I want to write software that deals with different text files and get those required values. I may not know the exact order but it must be a template like a bank statement or something.
Thanks!
It's a bit hard to understand what exactly is the problem.. If all you need to do is to split strings, here's a possible way to do it:
str = 'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234';
tokenized = strsplit(str,' ');
Name = strjoin([tokenized(3:4)],' ');
Number = tokenized{9};
Account = tokenized{end};
Alternatively, for splitting you could use regexp(...,'split') or regexp(...,'tokens');
I think you want regular expressions for this. Here's an example:
str = 'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234';
matches=regexp(str, 'your number is (\w+).*Bank ID # (\d+)', 'tokens');
matches{1}
ans =
'894Gfsf' '734234'
My suggestion would be to make a whole array of strings with sample patterns that you want to match, then build a set of regular expressions that collectively match all of your samples. Try each regexp in sequence until you find one that matches.
To do this, you will need to learn about regular expressions.