logo
down
shadow

extract part of a file path between pattern using awk


extract part of a file path between pattern using awk

By : Valentina Gomez
Date : November 21 2020, 11:01 PM
I wish did fix the issue. I see that your path parts are fixed, so no need to use regexes. Field separation does the trick:
code :
awk -F/ '{print $4,$7}' test.txt
awk -F/ '{a="???";b="???";for (i=0;i<NF;i++) {if (tolower($i)=="system") a= $(i+1); if (($i=="data") && (tolower($(i+2))=="sales")) b = $(i+1)}; print a,b}' test.txt


Share : facebook icon twitter icon
Simple way to extract part of a file path?

Simple way to extract part of a file path?


By : user566641
Date : March 29 2020, 07:55 AM
wish of those help I'm not very good at C, and I always get stuck on simple string manipulation tasks (that's why I love Perl!). , I think sscanf could be appropriate:
code :
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void test(char const* path) {
    int len;
    if(sscanf(path, "/Volumes/%*[^/]%n", &len) != EOF) {
        char *drive = malloc(len + 1);
        strncpy(drive, path, len);
        drive[len] = '\0';

        printf("drive is %s\n", drive);
        free(drive);
    } else {
        printf("match failure\n");
    }
}

int main() {
    test("/Volumes/Media/Foo");
    test("/Volumes/Media");
    test("/Volumes");
}
drive is /Volumes/Media
drive is /Volumes/Media
match failure
Copy/extract part of a File path?

Copy/extract part of a File path?


By : user3145311
Date : March 29 2020, 07:55 AM
To fix the issue you can do Here's a quick implementation that returns the TAIL of a path, including the specified number of elements. There's also a bit of demo of how to use it, and the results are exactly the ones you requested. Unfortunately I don't fully understand what transformations you're after: this might be exactly what you're after, or it might be something entirely wrong, that just happens to produce a result that looks like your sample:
code :
program Project25;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function ExtractPathTail(const OriginalPath:string; const PathElemCount:Integer):string;
var i, start, found_delimiters: Integer;
begin
  start := 0;
  found_delimiters := 0;
  for i:=Length(OriginalPath) downto 1 do
    if OriginalPath[i] = '\' then
    begin
      Inc(found_delimiters);
      if found_delimiters = PathElemCount then
      begin
        start := i;
        Break;
      end;
    end;
  if start = 0 then
    raise Exception.Create('Original path is too short, unable to cut enough elements from the tail.') // mangled English to help SO's code formatter
  else
    Result := System.Copy(OriginalPath, start+1, MaxInt);
end;

const SamplePath = 'D:\Programs\Tools\Bin\Somefile.dat';

begin
  try
    WriteLn('C:\Users\' + ExtractPathTail(SamplePath, 2)); // prints: C:\Users\Bin\Somefile.dat
    WriteLn('C:\Users\' + ExtractPathTail(SamplePath, 3)); // prints: C:\Users\Tools\Bin\Somefile.dat
    WriteLn('C:\Users\Programs\' + ExtractPathTail(SamplePath, 3)); // prints: C:\Users\Programs\Tools\Bin\Somefile.dat
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
How to extract part of the path and the ending file name with Regex?

How to extract part of the path and the ending file name with Regex?


By : Luur Luurman
Date : March 29 2020, 07:55 AM
will be helpful for those in need
code :
$path = '/whatever/path/length/public_html/wp-content/plugins/adminimize/languages/adminimize-en_US.mo';
preg_match('/plugins\/([^\/]+?)\/(?:[^\/]+\/)?(.+)/', $path, $matches);
$dir = $matches[1];
$filename = $matches[2];
R: extract part content from text file (e.g. sdf) according to certain pattern?

R: extract part content from text file (e.g. sdf) according to certain pattern?


By : Snups TM
Date : March 29 2020, 07:55 AM
With these it helps Read in the lines and create a grouping variable, g.all, having a component for each line of the file. That is, it is 1 for each line of the first group, 2 for each line of the second group and so on. Determine which group each of the desired strings are in giving g.ok and then extract all lines from those groups:
code :
L <- readLines("myfile")
g.all <- cumsum(grepl("^\\[", L))
g.ok <- g.all[grep("DB00035|DB00039", L)]
L[g.all %in% g.ok]
g.all <- rev(cumsum(grepl("$$$$", rev(L), fixed = TRUE)))
How to extract the final part of the file path in C?

How to extract the final part of the file path in C?


By : user2796821
Date : March 29 2020, 07:55 AM
like below fixes the issue Splice along a delimiter (here, "/"), and then grab the last element. Then create a string to use as the new filename based on that. Paraphrased from the link below;
code :
#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "strtok splits once per call, call many times to split full string";
    int init_size = strlen(str);
    char delim[] = "/";

    char *ptr = strtok(str, delim);

    while(ptr != NULL)
    {
        last = ptr //[p]oin[t]e[r]
        ptr = strtok(NULL, delim);
    }
    strcat("newfile.", last)

    //open a file with that name, write to it, etc.

    return 0;
}
temp = strcat(original_filename, newfile)
new_filename = strcat(temp, original_file_extenstion)
Related Posts Related Posts :
  • Find duplicated nested directories
  • Is it possible to several commands in the background but wait for all results and fail the script if an command fails
  • Cannot run script added to existing docker container
  • reading parameters into cURL from a text file in bash
  • Loop through all columns in Bash
  • replace some string after certain other string with sed
  • What does !!:2 mean in Mac or Linux?
  • Why does input redirection work differently when in command substitution?
  • Bash Script store cat output in variable and then echo it
  • Restore previous value or unset environment variable
  • string array is not assigned properly to each color
  • Can a bash script prepopulate the prompt with a command to run when it exits?
  • Getting a string from a line that follows a certain character
  • Nested bash command not working in teamcity ssh exec
  • Making wget a bash.sh
  • for loop in bash exiting to early without errors
  • Rename file and keep the extension in bash
  • Make GNU parallel return the server name in the sshlogin file
  • Bash check environment var is set fails doesn't work
  • command concatenation in bash
  • Extracting information with AWK and PATTERN
  • how to pass in a variable to awk commandline
  • Innobackupex call from bash script
  • How to remove lines appear only once in a file using bash
  • Bash Script Function
  • Search for numbers in files and replace the match with "result -1"
  • Rename file based on content
  • Insert part of filename to beginning of lines within file
  • How to backup an application's database
  • alias for cp with arguments
  • For loop deletes files Instead of renaming
  • Execute ssh shell script from npm command
  • Exclude certain string bash
  • Bash test value evaluation
  • Running bash script with parameter
  • Bash - Count number of occurences in textfile and display in descending order
  • Bash: find files within file size range and report their names to a file
  • I have many single files nested within many folders. Is there a way in Mac OSX to extract all the files into one folder?
  • Create a note in privnote.com using HTTP requests
  • Recursively find hexadecimal bytes in binary files
  • How to kill a background job run by bash file
  • How to strip or replace all matches of a pattern from a string in Bash?
  • How can I run executable in shell script?
  • How do I pass BASH host command a variable and assign the answer to another variable
  • Change permissions of file in CMake
  • update stored variables in shell script
  • Grep + sed combination
  • Find a line with certain string then remove it's newline character at the end in bash
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com